Simpler form std :: unordered_map :: insert?

Is there an easier way to check if the call to std::unordered_map::insert succeeded in writing this mammoth block of code?

 std::pair< T1, T2 > pair(val1, val2); std::pair< std::unordered_map< T1, T2 >::const_iterator, bool> ret = _tileTypes.insert(pair); if(!ret.second) { // insert did not succeed } 
+6
source share
2 answers

How about just:

 if(!_tileTypes.insert(std::make_pair(val1, vla2)).second) { // insert did not succeed } 
+12
source
 if (!_tileTypes.insert(pair).second) 

?

Alternatively, typedefs can be useful to tidy up such things.

Also, if you are using C ++ 11, you can use the auto keyword to type output:

 auto ret = _tileTypes.insert(pair); 
+11
source

All Articles