Javatpoint Logo
Javatpoint Logo

C++ map at() Function

C++ map at() function is used to access the elements in the map with the given key value. It throws an exception out_of _range, if the accessed key is not present in the map.

Syntax

Consider the key value k, syntax would be:

Parameter

k: Key value of the element whose mapped value is to be accessed.

Return value

It returns a reference to the mapped value of the element with a key value.

Example 1

Let's see a simple example for accessing the elements.

Output:

A: 10
B: 20	
C: 30

In the above, at() function is used to access the elements of map.

Example 2

Let's see a simple example to add the elements using their key values.

Output:

JavaTPoint 

In the above example, at() function is used to add the elements after initialization using the associated key values.

Example 3

Let's see a simple example to change the value associated with the key value.

Output:

Elements are:
100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman

Elements after make changes are:
100: Nidhi
200: Deep
300: Pinku
400: Suman
500: Arohi

In the above example, at() function is used to change the values associated with their key values.

Example 4

Let's see a simple example to handle the ?out of range? exception.

Output:

Java
C++
Python
Out of Range Exception at map::at

The above example throws an out_of_range Exception since there is no key with value z in the map.

Next TopicC++ Map



Help Others, Please Share

facebook twitter pinterest