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.
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:
If you simply want to know if it’s in there, without using it, do:
At the very least, don’t use a macro! There’s no reason to in C++:
But there’s no use for this, just use
count.