tags:

views:

71

answers:

2

I'm using the following code in some cases:

#define array_key_exists(find_key, arr) (arr.find(find_key) != arr.end())

But i also use simply this method:

if(SomeMap["something"]){
    // key exists
}

I am using String to int map.

Are they both as fast...? Or is there a possibility for errors with the second case, assuming i am not using zero value in the map values at all? So far the second case seems to work just fine.

A: 

Use find and compare against end iterator like the first method. The second method will insert an empty element into the map if the key didn't exist.

Peter G.
Does it insert a new empty element every time the key didnt exist? So the memory use can go higher ang higher?
Newbie
@Newbie: No, because it only makes it if it's not there. After the first time, it'll return the existing element.
GMan
But if i check million random strings, it will create million random elements into the map and set their values to zero because they didnt exist?
Newbie
@Newbie: Right. The `operator[]` is: "get element and return it". It isn't for checking if an element is there.
GMan
So, to leave no doubts, the operator[] will be slower than my first example?
Newbie
@Newbie: As a beginner efficiency is your last concern. You should be focused on getting working, clear code. Asking of it's faster or slower is meaningful since it does something different. That's like asking if it's faster to drive a car or eat an apple.
GMan
I am not that noob i cant get it working :D it works fine, i just want to do things the best way from beginning... (i just finished rewriting 20k lines of code...)If i dont care about performance, every "meaningless optimization" will add up and make the program fucking slow, and fixing that will take the whole program rewrite
Newbie
@Newbie: No, it won't. Good clean code solves 90% of optimization problems. It's *very* easy to optimize clean, well-written code (hardly a "whole- program rewrite", while conversely it's difficult to optimize poorly written code (which is harder to just get working int he first place!). The cleanest and most maintainable methods tend to line up with most efficient as well, *so focus on good code, not fast code*, please. You've already shown optimization is *not* a main concern, because if it were you'd of known about profiling. The only way to optimize correctly is to *profile* and get...
GMan
...real data to make real optimizations, and not just make random guesses about things. Again, profiling (optimizing) clean code is very easy, while doing the same with ugly code is very hard. **Good code first, fast code last.** Make your code *work* before you worry about how well it works; what's the point in asking "which is faster?" when the code is broken?
GMan
I get your point, i just need to know details, well, i go test it myself then. Nevertheless, thanks for help.
Newbie
+7  A: 

The second if-statement will always be entered, because if the key doesn't exist it will be created. (After which, subsequent calls will just return the existing element.)

If you want to find a value and use it if it exists, you typically do this:

std::map<T>::iterator iter = theMap.find(theKey);
if (iter != theMap.end())
{
    // use iter
}
else
{
    // value doesn't exist
}

If you simply want to know if it's in there, without using it, do:

if (theMap.count(theKey)) // in map, count will return either 0 or 1
{
    // it exists
}
else
{
    // it doesn't exist
}

At the very least, don't use a macro! There's no reason to in C++:

template <typename Map, typename Key>
bool contains(const Map& pMap, const Key& pKey)
{
    return pMap.find(pKey) != pMap.end();
}

But there's no use for this, just use count.

GMan
doesnt the .count() be very slow as it counts them all? And why is using macros bad?
Newbie
I like the fact that you explained every solution. Great effort Mr.GMan.
@Newbie: Where do you get the notion it counts them all? For `map`, `count` will literally just do: `return find(key) != end();`.
GMan
"count", i guess its misleading function name then :P
Newbie
Btw, why is using macro bad in my case? doesnt templates make bigger executables?
Newbie
@Newbie: Again, you should be focused on good, clean, safe, and maintainable code, not pre-mature optimization. Macro's are not type-safe and are pretty ugly, while functions are typesafe and, with templates, just as generic. And probably not, any compiler worth a grain of sand will optimize a small template function so you'd end up generating the exact same code.
GMan