In C++, std::map::at() gives you a reference to the value stored for a key. You can read the value, or assign to it to change it. It is available since C++11.
at() is the safe way to read a map. If the key is not in the map, it throwsstd::out_of_range. The m[key] operator is different: it quietly adds the key with a default 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
Call at with the key you want. The key must already be in the map.
C++
int a = m.at("Raj"); // read the value of key "Raj"
m.at("Raj") = 26; // change the value of key "Raj"
Call
What it does
m.at(key)
Returns a reference to the value for key. Throws std::out_of_range if the key is missing.
cm.at(key)
On a const map it returns a const reference, so you can read but not change the value.
at() never inserts. Use m[key] only when you want a missing key to be created with a default value (0 for numbers, an empty string for std::string).
stock.at("kiwi") cannot find the key, so it throws std::out_of_range. The line after it is skipped and control jumps to the catch block.
The size is still 2. Unlike operator[], a failed at() does not add a "kiwi" entry.
Example 3 — at() Compared with operator[]
See that at() only changes existing keys, while m["c"] creates a new one.
C++
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
int main() {
std::map<std::string, int> m = {{"a", 1}, {"b", 2}};
m.at("a") = 10; // changes an existing key
m["c"] = 3; // creates the new key "c"
std::cout << "size after []: " << m.size() << '\n';
try {
m.at("d") = 4;
} catch (const std::out_of_range&) {
std::cout << "at() did not add d\n";
}
for (const auto& kv : m) {
std::cout << kv.first << "=" << kv.second << ' ';
}
std::cout << '\n';
return 0;
}
Output
size after []: 3
at() did not add d
a=10 b=2 c=3
How It Works
m.at("a") = 10 assigns through the returned reference, so the value of the existing key "a" becomes 10.
m["c"] = 3 does not find "c", so the operator inserts it first and then assigns. The size grows from 2 to 3.
m.at("d") throws and the catch prints a message, so "d" is never added. The loop prints the pairs in key order.
Frequently Asked Questions
It returns a reference to the value stored for a key. You can read it or assign a new value to it. It works only for keys that are already in the map.
at() throws std::out_of_range. Wrap the call in try and catch, or check first with contains() or find().
at() throws when the key is missing and never changes the size. operator[] inserts the missing key with a default value, so reading a map with [] can silently make it bigger. at() also works on a const map, but [] does not.
No. at() only reaches existing keys. To add a key use insert(), emplace(), try_emplace(), insert_or_assign(), or operator[].
Yes. A map is a balanced tree, so at() takes logarithmic time: about 20 steps for a million keys.