As long as the map is not a multimap, one of the most elegant ways would be to use the count method
if (m.count(key))
// key exists
The count would be 1 if the element is indeed present in the map.
More Related Contents:
- What requirements must std::map key classes meet to be valid keys?
- remove_if equivalent for std::map
- Using a STL map of function pointers
- Inspecting standard container (std::map) contents with gdb
- std::map, pointer to map key value, is this possible?
- Reverse map lookup
- What’s the difference between “STL” and “C++ Standard Library”?
- std::vector::resize() vs. std::vector::reserve()
- How to convert wstring into string?
- Why use iterators instead of array indices?
- How do I erase an element from std::vector by index?
- Can standard container templates be instantiated with incomplete types?
- Is list::size() really O(n)?
- Sort list using STL sort function
- Heterogeneous containers in C++
- What does the standard library guarantee about self move assignment?
- How to filter items from a std::map? [duplicate]
- Is the use of std::vector objects in C++ acceptable, or should I use an alternative?
- std::optional specialization for reference types
- How do I store arrays in an STL list?
- Can we use a user defined class for the key in a STL map?
- Are const_iterators faster?
- How to use the priority queue STL for objects?
- Why the libc++ std::vector internally keeps three pointers instead of one pointer and two sizes?
- Visual c++ “for each” portability
- Is stl vector concurrent read thread-safe?
- Should you overload swap in the std namespace?
- Missing C++ header after updating OSX Command Line Tools 6.3
- const auto std::initializer_list difference between Clang and GCC
- What C++ pitfalls should I avoid? [closed]