🌐
cppreference.com
en.cppreference.com › cpp › container › map
std::map - cppreference.com
From cppreference.com · < cpp | container · C++ [edit] Containers library · [edit] std::map · [edit] std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations ...
🌐
cppreference.com
en.cppreference.com › cpp › container › multimap
std::multimap - cppreference.com
std::multimap is an associative container that contains a sorted list of key-value pairs, while permitting multiple entries with the same key. Sorting is done according to the comparison function Compare, applied to the keys. Search, insertion, and removal operations have logarithmic complexity ...
🌐
Lsu
ld2014.scusa.lsu.edu › cppreference › en › cpp › container › map.html
std::map - cppreference.com
From cppreference.com · < cpp‎ | container · C++ Containers library · std::map · std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations have logarithmic ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › map-associative-containers-the-c-standard-template-library-stl
Map in C++ STL - GeeksforGeeks
Explanation: Above program demonstrates how to create and initialize a map in C++ using key–value pairs.
Published   May 11, 2026
🌐
Cplusplus
cplusplus.com › reference › map › map
std::map
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. In a map, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key.
🌐
Cppreference
cppreference.com › Template:cpp › container › map › navbar
Template:cpp/container/map/navbar - cppreference.com
From cppreference.com · C++ [edit] Containers library · [edit] std::map · [edit] Support us · Recent changes · FAQ · Offline version · What links here · Related changes · Upload file · Special pages · Printable version · Permanent link ·
🌐
Cppreference
cppreference.com
cppreference.com
C++ reference C++11, C++14, C++17, C++20, C++23, C++26, C++29 │ Compiler support C++11, C++14, C++17, C++20, C++23, C++26, C++29 · Preprocessor − Comments ASCII chart Basic concepts Keywords Names (lookup) Types (fundamental types) The main function Modules (C++20) Contracts (C++26) ...
🌐
cppreference.com
en.cppreference.com › cpp › container › map › swap
std::map<Key,T,Compare,Allocator>::swap - cppreference.com
#include <iostream> #include <string> #include <utility> #include <map> // print out a std::pair template<class Os, class U, class V> Os& operator<<(Os& os, const std::pair<U, V>& p) { return os << p.first << ':' << p.second; } // print out a container template<class Os, class Co> Os& operator<<(Os& os, const Co& co) { os << '{'; for (auto const& i : co) os << ' ' << i; return os << " }\n"; } int main() { std::map<std::string, std::string> m1{{"γ", "gamma"}, {"β", "beta"}, {"α", "alpha"}, {"γ", "gamma"}}, m2{{"ε", "epsilon"}, {"δ", "delta"}, {"ε", "epsilon"}}; const auto& ref = *(m1.beg
🌐
cppreference.com
en.cppreference.com › cpp › container › map › max_size
std::map<Key,T,Compare,Allocator>::max_size - cppreference.com
November 29, 2021 - From cppreference.com · < cpp | container | map · [edit template] C++ [edit] Containers library · [edit] std::map · [edit] Returns the maximum number of elements the container is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the ...
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › cpp › container › map › empty.html
std::map<Key,T,Compare,Allocator>::empty - cppreference.com
November 29, 2021 - #include <iostream> #include <map> ... { std::map<int,int> numbers; std::cout << std::boolalpha; std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n'; numbers.emplace(42, 13); numbers.insert(std::make_pair(13317, 123)); std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n'; } ... Retrieved from "https://en.cppreference.com/mwiki...
🌐
Cppreference
en.cppreference.com › w › cpp › container › map › value_comp.html
std::map<Key,T,Compare,Allocator>::value_comp - cppreference.com
#include <iostream> #include <map> #include <utility> // Example module 97 key compare function struct ModCmp { bool operator()(int lhs, int rhs) const { return (lhs % 97) < (rhs % 97); } }; int main() { std::map<int, char, ModCmp> cont; cont = {{1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'}, {5, 'e'}}; auto comp_func = cont.value_comp(); for (const std::pair<int, char> val = {100, 'a'}; auto it : cont) { const bool before = comp_func(it, val); const bool after = comp_func(val, it); std::cout << '(' << it.first << ',' << it.second << ") "; if (!before && !after) std::cout << "equivalent to key (" << val.first << ")\n"; else if (before) std::cout << "goes before key (" << val.first << ")\n"; else if (after) std::cout << "goes after key (" << val.first << ")\n"; else std::unreachable(); } }
🌐
cppreference.com
en.cppreference.com › cpp › container › mdspan › layout_right › mapping
std::layout_right::mapping - cppreference.com
December 23, 2024 - From cppreference.com · < cpp ... · [edit] The class template layout_right::mapping controls how multidimensional indices are mapped in a row-major manner to a one-dimensional value representing the offset....
🌐
cppreference.com
en.cppreference.com › cpp › language › types
Fundamental types - cppreference.com
long double — extended precision floating-point type. Does not necessarily map to types mandated by IEEE-754.
🌐
Cppreference
en.cppreference.com › w › cpp › container › map › get_allocator.html
std::map<Key,T,Compare,Allocator>::get_allocator - cppreference.com
November 29, 2021 - From cppreference.com · < cpp‎ | container‎ | map · [edit template] C++ [edit] Containers library · [edit] std::map · [edit] Returns the allocator associated with the container. The associated allocator. Constant. Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cp...
🌐
Cppreference
en.cppreference.com › w › cpp › container › mdspan › layout_left › mapping.html
std::layout_left::mapping - cppreference.com
December 23, 2024 - From cppreference.com · < cpp‎ ... · [edit] The class template layout_left::mapping controls how multidimensional indices are mapped in a column-major manner to a one-dimensional value representing the offset....
🌐
cppreference.com
en.cppreference.com › cpp › container › map › operator_cmp
operator==,!=,<,<=,>,>=,<=>(std::map) - cppreference.com
November 29, 2021 - From cppreference.com · < cpp | container | map · [edit template] C++ [edit] Containers library · [edit] std::map · [edit] Compares the contents of two maps. 1,2) Checks if the contents of lhs and rhs are equal, that is, they have the same number of elements and each element in lhs compares ...
🌐
cppreference.com
en.cppreference.com › cpp › container › map › map
std::map<Key,T,Compare,Allocator>::map - cppreference.com
November 29, 2021 - #include <iomanip> #include <iostream> #include <map> #include <string> template<typename Key, typename Value, typename Cmp> std::ostream& operator<<(std::ostream& os, std::map<Key, Value, Cmp> const& map) { os << "{ "; for (auto comma{map.size()}; auto const& p : map) os << '\'' << p.first << "' is " << p.second << (--comma ?
🌐
Cppreference
en.cppreference.com › w › cpp › header › map.html
Standard library header <map> - cppreference.com
November 27, 2023 - namespace std { template<class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key, T>>> class map { public: // types using key_type = Key; using mapped_type = T; using value_type = pair<const Key, T>; using key_compare = Compare; using allocator_type = Allocator; using pointer = typename allocator_traits<Allocator>::pointer; using const_pointer = typename allocator_traits<Allocator>::const_pointer; using reference = value_type&; using const_reference = const value_type&; using size_type = /* implementation-defined */; using difference_type = /* implementation-d
Top answer
1 of 5
27

So long as the object in question isn't removed from the map, then yes it is safe. Once inserted into a map objects don't move around even if other elements are added or removed.

object& obj = objmap.find(num)->second;

This is potentially dangerous unless you are sure that an element with key num actually exists in the map. If you are not sure, you could use the overload of insert that returns an iterator and a bool which indicates whether a new element was inserted or an element with the given key was already present in the map.

E.g.

object& obj = objmap.insert( std::make_pair(num, object(arg1, arg2, argN)) ).first->second;
2 of 5
13

This is safe as long as the element isn't removed from the map.

However, the second line is not quite safe :

object& obj = objmap.find(num)->second;

If there is no elements with key num in the map, find will return objmap.end(). This possibility should be tested before dereferencing the returned iterator :

const std::map<int, object>::iterator it = objmap.find(num);
if (it != objmap.end())
{
    object& obj = it->second;
    /* ... */
}

Now, if the goal isn't really to find but really to insert, calling operator[] is a possibility (although, as you already noticed, it requires the value to provide a parameterless constructor). But you have to understand that these are two very different things :

  • find only finds : if the key isn't found, nothing gets inserted and the end iterator is returned
  • operator[] always returns a reference to a value in the map : were the key absent, an insertion occurs (for a default constructed value : thus the constructor requirement)
🌐
SACO Evaluator
saco-evaluator.org.za › docs › cppreference › en › cpp › container › map › ~map.html
std::map::~map - cppreference.com
From cppreference.com · < cpp‎ | container‎ | map · C++ Containers library · std::map · Destructs the container. The destructors of the elements are called and the used storage is deallocated. Note, that if the elements are pointers, the pointed-to objects are not destroyed.