Obviously you can write your own getValue() routine if you want (also in C++, there is no reason to use out), but I would suspect that once you get the hang of using std::map::find() you won't want to waste your time.

Also your code is slightly wrong:

m.find('2'); will search the map for a keyvalue that is '2'. IIRC the C++ compiler will implicitly convert '2' to an int, which results in the numeric value for the ASCII code for '2' which is not what you want.

Since your keytype in this example is int you want to search like this: m.find(2);

How so? find indicates intent far better than count does. More over, count doesn't return the item. If you read the OP's question, he's wants to check for the existance, and return the element. find does that. count does not.
–
AlanMay 23 '13 at 16:49

The key exists, and then what? At that point you'd usually want to get the value for it, paying for another search (e.g. using operator[]). find gives you .NET's TryGetValue semantics, which is almost always what you (and specifically the OP) want.
–
Ohad SchneiderNov 6 '14 at 15:58

I recommend you get used to it rather than trying to come up with a new mechanism to simplify it. You might be able to cut down a little bit of code, but consider the cost of doing that. Now you've introduced a new function that people familiar with C++ won't be able to recognize.

If you want to implement this anyway in spite of these warnings, then: