TITLE: common mistake in checking for entries in STL map (Source: Mumit Khan's STL Newbie Guide, http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html) Checking for an item in a map This is from a bug we found in our code a while back. typedef Map > XMap; XMap xmap; // // populate xmap will what-not. // const X* xx = xmap[5]; if (xx == 0) { // not in map. do_something(); } else { do_something_else(); } looks pretty innocuous, but what really happens is that a new entry for xmap[5] is created and gets stuffed with a NULL pointer which causes amazing amount of headache 10,000 lines of code later. The right way of course (and documented in the fine manual) is the following: typedef Map > XMap; XMap xmap; // // populate xmap will what-not. // XMap::const_iterator it = xmap.find(5); if (it == xmap.end()) { // not in map. do_something(); } else { do_something_else(); }