C++ std::map at() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::map

Definition

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 throws std::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"
CallWhat 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).

Library declarations
C++
T& at(const Key& key);
const T& at(const Key& key) const;

Parameters

ParameterDescription
keyThe key to look up. It is compared with the stored keys using the map's comparison, which is std::less by default.

Return Value

Return ValueDescription
T&A reference to the value that belongs to key. For a const map, a const T&.

C++ Examples

Example 1 — at() Reads a Value

Store ages in a map and read two of them by name.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}, {"Zoe", 30}};

    std::cout << "Raj: " << age.at("Raj") << '\n';
    std::cout << "Zoe: " << age.at("Zoe") << '\n';
    return 0;
}

How It Works

  1. The map holds three pairs. Each name is a key, and the age is the value stored for that key.
  2. age.at("Raj") searches the keys and returns a reference to the value of "Raj", which is 25.
  3. age.at("Zoe") works the same way and gives 30. Nothing in the map changes.

Example 2 — at() Throws for a Missing Key

Ask for a key that does not exist and catch the exception that at() throws.

C++
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>

int main() {
    std::map<std::string, int> stock = {{"apple", 5}, {"pear", 2}};

    try {
        std::cout << stock.at("apple") << '\n';
        std::cout << stock.at("kiwi") << '\n';
        std::cout << "not printed\n";
    } catch (const std::out_of_range&) {
        std::cout << "kiwi is not in the map\n";
    }

    std::cout << "size: " << stock.size() << '\n';
    return 0;
}

How It Works

  1. stock.at("apple") finds the key and prints 5.
  2. 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.
  3. 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;
}

How It Works

  1. m.at("a") = 10 assigns through the returned reference, so the value of the existing key "a" becomes 10.
  2. m["c"] = 3 does not find "c", so the operator inserts it first and then assigns. The size grows from 2 to 3.
  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.

Next: begin()

Keep going through the std::map functions. Get an iterator to the pair with the smallest key.

begin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful