The C++ map at method is employed to retrieve the elements in the map using the specified key value. It raises an outofrange exception if the requested key is not found within 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 : The key value of the element that is to be used to access the corresponding mapped value.
Return value
It provides a pointer to the mapped value of the item associated with a specific key.
Example 1
Let's explore a basic illustration 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 previous example, the at method is employed to retrieve elements from a map data structure.
Example 2
Let's examine a basic illustration of incorporating elements by their respective 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 example provided, the at method is employed to insert elements after the initialization using corresponding key values.
Example 3
Let's examine a basic illustration of modifying the data linked 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 provided example, the at method is utilized to update the values linked with their respective key values.
Example 4
Let's examine a basic example demonstrating how to manage 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 example mentioned above triggers an outofrange Exception because there is no key with the value 'z' present in the map.