Skip to content
Related Articles
Open in App
Not now

Related Articles

unordered_map at() in C++

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 26 Sep, 2021
Improve Article
Save Article

Prerequisite : Unordered maps in STL
Unordered_map : unordered_map is an associated container that stores elements formed by the combination of key value and a mapped value. The key value is used to uniquely identify the element and mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined.
unordered_map :: at(): This function in C++ unordered_map returns the reference to the value with the element as key k. 
 

Syntax:  

unordered_map.at(k);
Parameter:
It is the key value of the element whose mapped value we want to access.
Return type :
A reference to the mapped value of the element with a key value equivalent

Note : The method gives run-time error if key is not present.

CPP




// C++ program to illustrate
// std :: unordered_map :: at()
#include<iostream>
#include<string>
#include<unordered_map>
 
using namespace std;
 
int main()
{
    unordered_map<string,int> mp = {
                            {"first",1},
                            {"second",2},
                            {"third",3},
                            {"fourth",4}
    };
                                     
    // returns the reference i.e. the mapped
    // value with the key 'second'
    cout<<"Value of key mp['second'] = "
        <<mp.at("second")<<endl;
     
    try
    {
        mp.at();
    }
 
    catch(const out_of_range &e)
    {
        cerr << "Exception at " << e.what() << endl;
    }
     
     
    return 0;
}

Output:  

Value of key mp['second'] = 2
Exception at _Map_base::at

Practical Application : std :: unordered_map :: at() function can be used to access the mapped value and thus can edit, update etc. 

CPP




// CPP program to illustrate
// application of this function
// Program to correct the marks
// given in different subjects
#include<iostream>
#include<string>
#include<unordered_map>
 
using namespace std;
 
// driver code
int main()
{
    // marks in different subjects
    unordered_map<string,int> my_marks = {
                    {"Maths", 90},
                    {"Physics", 87},
                    {"Chemistry", 98},
                    {"Computer Application", 94}
                    };
         
                                     
        my_marks.at("Physics") = 97;
        my_marks.at("Maths") += 10;
        my_marks.at("Computer Application") += 6;
         
        for (auto& i: my_marks)
        {
            cout<<i.first<<": "<<i.second<<endl;
        }
     
     
     
    return 0;
}

Output:  

Computer Application: 100
Chemistry: 98
Physics: 97
Maths: 100

How unordered_map at() is different from unordered_map operator() 

  • Both at() and operator[] is used to refer the element present at the given position, the only difference is, at() throws out-of-range exception whereas operator[] shows undefined behavior i.e. if operator[] is used to find the value corresponding to key and if key is not present in unordered map, it will first insert the key into the map and then assign the default value ‘0’ corresponding to that key. . 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!