Logo

Programming-Idioms

History of Idiom 51 > diff from v27 to v28

Edit summary for version 28 by programming-idioms.org:
[Cpp] Fixed logic == -> !=

Version 27

2016-10-28, 08:18:38

Version 28

2016-10-29, 13:24:43

Idiom #51 Check if map contains key

Determine whether map m contains an entry for key k

Illustration

Idiom #51 Check if map contains key

Determine whether map m contains an entry for key k

Illustration
Extra Keywords
table dictionary hash
Extra Keywords
table dictionary hash
Imports
#include <map>
Imports
#include <map>
Code
bool key_exists = _m.find(_k) == _m.end();
Code
bool key_exists = m.find(_k) != m.end();
Comments bubble
The map is a std::map. If value is not found, "find" returns end(); else returns the element.
Comments bubble
m is a std::map. If value is not found, "find" returns end(); else returns the element.
Doc URL
http://www.cplusplus.com/reference/map/map/find/
Doc URL
http://www.cplusplus.com/reference/map/map/find/
Demo URL
https://goo.gl/UFCqBw
Demo URL
https://goo.gl/UFCqBw