In C++, std::map::insert()adds a new pair to a std::map. The pair goes to the right place so that the keys stay sorted.
If the key is already in the map, insert() does nothing and the old value stays. The m[key] = value form is different: it creates the key if needed and always overwrites the value. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax
Pass a pair, a braced pair, an initializer list, or a range of pairs.
C++
auto r = m.insert({"one", 1}); // one pair
m.insert(std::make_pair("two", 2)); // pair made with make_pair
m.insert({{"c", 3}, {"d", 4}}); // initializer list
m.insert(other.begin(), other.end()); // a range of pairs
Call
What it does
m.insert(pair)
Adds the pair if its key is new. Returns std::pair<iterator, bool>.
m.insert(hint, pair)
Same, but uses hint as a starting position to be faster. Returns an iterator.
m.insert(first, last)
Inserts every pair from the range. Pairs with keys that already exist are skipped.
m.insert({p1, p2, ...})
Inserts all pairs in the initializer list, skipping duplicate keys.
m.insert(std::move(node))
Inserts a node handle from extract() (C++17).
Since C++17 you also have insert_or_assign() to update existing values and try_emplace() to avoid wasted work. emplace() builds the pair in place.
The list form adds c and a. The pair {"b", 99} is skipped because "b" already exists, so b stays 2.
The range form copies pairs from other. d is new and added. {"a", 50} is skipped because a is already 1.
The map keeps keys sorted, so the loop prints a, b, c, d even though they were inserted in a mixed order.
Example 3 — insert() Compared with operator[]
See that insert() keeps old values, that [] overwrites them, and that reading with [] can add a key.
C++
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> m;
m.insert({"x", 1});
m.insert({"x", 2}); // ignored
m["y"] = 1;
m["y"] = 2; // overwrites
int q = m["q"]; // only reading, but q is added with 0
std::cout << "q = " << q << ", size = " << m.size() << '\n';
for (const auto& kv : m) {
std::cout << kv.first << "=" << kv.second << ' ';
}
std::cout << '\n';
return 0;
}
Output
q = 0, size = 3
q=0 x=1 y=2
How It Works
insert({"x", 2}) is ignored because "x" exists, so x keeps the value 1.
m["y"] = 2 overwrites the earlier 1, because the bracket operator assigns every time.
m["q"] does not find "q", so it inserts it with the default 0 even though you only read it. That is why the size is 3 and q=0 appears.
Frequently Asked Questions
The single-pair form returns std::pair<iterator, bool>. The iterator points at the pair with that key, and the bool is true only if a new pair was added.
Nothing. The map is not changed and the old value stays. The returned bool is false.
insert() adds only new keys and keeps old values. m[key] = value adds the key if needed and always sets the value. Also, m[key] on a missing key inserts a default value even when you only read it.
Use m[key] = value, m.at(key) = value, or insert_or_assign() in C++17. insert() will not update it.
Yes. The pair is placed so the keys stay in order. You do not sort a std::map yourself.