C++ map at function is used to access the elements in the map with the given key value . It throws an exception outof range , if the accessed key is not present in the map.
Syntax
Consider the key value k , syntax would be:
mapped_type& at (const key_type& k);
const mapped_type& at (const key_type& k) const;
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.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<string,int> m = {
{ "A", 10 },
{ "B", 20 },
{ "C", 30 } };
for (auto& x: m) {
cout << x.first << ": " << x.second << '\n';
}
return 0;
}
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.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<int,string> mymap = {
{ 101, "" },
{ 102, "" },
{ 103, ""} };
mymap.at(101) = "Java";
mymap.at(102) = "T";
mymap.at(103) = "Point";
// prints value associated with key 101, i.e. Java
cout<<mymap.at(101);
// prints value associated with key 102, i.e T
cout<<mymap.at(102);
// prints value associated with key 103, i.e Point
cout<<mymap.at(103);
return 0;
}
Output:
Cpp Tutorial
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.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<int,string> mymap = {
{ 100, "Nikita"},
{ 200, "Deep" },
{ 300, "Priya" },
{ 400, "Suman" },
{ 500, "Aman" }};
cout<<"Elements are:" <<endl;
for (auto& x: mymap) {
cout << x.first << ": " << x.second << '\n';
}
mymap.at(100) = "Nidhi"; // changes the value associated with key 100 to Nidhi
mymap.at(300) = "Pinku"; // changes the value associated with key 300 to Pinku
mymap.at(500) = "Arohi"; // changes the value associated with key 500 to Arohi
cout<<"\nElements after make changes are:" <<endl;
for (auto& x: mymap) {
cout << x.first << ": " << x.second << '\n';
}
return 0;
}
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.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<char,string> mp = {
{ 'a',"Java"},
{ 'b', "C++" },
{ 'c', "Python" }};
cout<<endl<<mp.at('a');
cout<<endl<<mp.at('b');
cout<<endl<<mp.at('c');
try {
mp.at('z');
// since there is no key with value z in the map, it throws an exception
} catch(const out_of_range &e) {
cout<<endl<<"Out of Range Exception at "<<e.what();
}
Output:
Java
C++
Python
Out of Range Exception at map::at
The above example throws an outofrange Exception since there is no key with value z in the map.