public member function
<map>

std::map::at

      mapped_type& at (const key_type& k);const mapped_type& at (const key_type& k) const;
Access element
Returns a reference to the mapped value of the element identified with key k.

If k does not match the key of any element in the container, the function throws an out_of_range exception.

Parameters

k
Key value of the element whose mapped value is accessed.
Member type key_type is the type of the keys for the elements in the container, defined in map as an alias of its first template parameter (Key).

Return value

A reference to the mapped value of the element with a key value equivalent to k.

If the map object is const-qualified, the function returns a reference to const mapped_type. Otherwise, it returns a reference to mapped_type.

Member type mapped_type is the type to the mapped values in the container (see map member types). In map this is an alias of its second template parameter (T).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// map::at
#include <iostream>
#include <string>
#include <map>

int main ()
{
  std::map<std::string,int> mymap = {
                { "alpha", 0 },
                { "beta", 0 },
                { "gamma", 0 } };

  mymap.at("alpha") = 10;
  mymap.at("beta") = 20;
  mymap.at("gamma") = 30;

  for (auto& x: mymap) {
    std::cout << x.first << ": " << x.second << '\n';
  }

  return 0;
}

Possible output:
alpha: 10
beta: 20
gamma: 30


Complexity

Logarithmic in size.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).
The mapped value that is accessed may be modified by the caller. Concurrently accessing or modifying other elements is safe.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.
It throws out_of_range if k is not the key of an element in the map.

See also