Updated April 1, 2023
Introduction to C++ map at()
Maps are containers that have an association with the elements present inside the container and are somewhat designed in a mapped fashion. Each element has its map and the map contains a key value and a mapped value. if any two values inside a map are the same then they can never be mapped. Mapped at() which means the at() function associated will be used as a reference element for referring the element mapped to the key-value given as a parameter to the at() function and will be returning a string with that defined element. In this topic, we are going to learn about C++ map at().
Syntax
Map_name.at(key_value)
The syntax flow is arranged in a way in which the map is named with some name and then the key value is the parameter that will be used to fetch the key-value mapped to that element. The return value is the directly referred element pointing at that given key value. There are some values or the elements and the condition in which the errors and exception can occur which will result in the error and exception like it will throw an exception like out_of_range if the key is not present in the defined range. Except for the guaranteed throws exception, there is no other strong exception.
How C++ map at() Function Works?
Every function has its own working pattern so do the C++ map at() function has, let’s revolve around its working.
At() function associated with the C++ Map first checks for the range of the container and then clearly throws an exception saying elements not in range, while operator on other hand does not check for the range of container and shows an undefined behavior whenever an element is not in the range to access the elements simultaneously in the series. Also, there is a mere difference between the actual map at() function and then the operator as mentioned operators never check for the elements within some defined range rather gives an undefined behavior whenever an element not in the range is accessed.
One key value is associated with the at the function which tried to access the elements being defined inside the map once the key-value lies in the range and satisfies all the condition following the data name and at() function then the necessary key value gets returned. Also, it highly depends on the range of the map whether the key in the data map is satisfactory. If it is not, then again there is a chance during execution time to return some exception or error saying out of range or not lying in the defined range.
If the map object is defined as constant, then the function returns a reference to the mapped element or the data structure. If this condition also gets satisfied, then again just to recollect the mapped type element will be the return type.
Its time complexity also is defined as its logarithmic value in size. There is no change in the iterator validity. And the data or elements present depends on the races being conducted at the time of execution in a way that let us say the container contains a set of elements in the data structure as Map under a condition this container is accessed like neither the constant nor the constant versions can be modified then the mapped value which is accessed may get modified by the caller. Simultaneous access or the modification of the other elements is very safe.
Coming to the exception or the error-free function no changes should be performed in the container otherwise it will throw an error out_of_range or exception as an error.
Map operator and map find are part of map functionality which differs from the working of Map at() functionality.
Examples of C++ map at()
Here are the following examples to implement C++ map at() function mentioned below.
Example #1
Program to illustrate the Map at() function with at() as a function associated with map taking some numbers as input.
Code:
#include <iostream>
#include <string>
#include <map>
int main ()
{
std::map<std::string,int> mymap = {
{ "integer", 0 },
{ "number", 0 },
{ "symbols", 0 } };
mymap.at("integer") = 10 ;
mymap.at("number") = 2;
mymap.at("symbols") = 5;
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << '\n';
}
return 0;
}
Output:
Example #2
Program to demonstrate the error and exception when the defined map element is out of range with exception and error as out_of_range.
Code:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, int> mymap;
mymap["welcome"] = 1;
mymap["to"] = 2;
mymap["the"] = 3;
mymap["educba learning platform"] = 4;
cout << mymap.at("thanks for visiting");
return 0;
}
Output:
Example #3
Program to demonstrate the element access with the number of the digit accessibility as an element defined.
Code:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<string, int> mymap;
mymap["hi"] = 5;
mymap["welcome"] = 6;
mymap["to"] = 7;
mymap["portal"] = 8;
cout << mymap.at("welcome");
return 0;
}
Output:
Example #4
Program to demonstrate the map operator() which functions somewhat similar with a mere difference to the map at() function.
Code:
#include <iostream>
#include <map>
#include <string>
int main ()
{
std::map<char,std::string> mymap;
mymap['p']="first element";
mymap['q']="second element";
mymap['r']=mymap['q'];
std::cout << "mymap['p'] is " << mymap['p'] << '\n';
std::cout << "mymap['q'] is " << mymap['q'] << '\n';
std::cout << "mymap['r'] is " << mymap['r'] << '\n';
std::cout << "mymap['s'] is " << mymap['s'] << '\n';
std::cout << "mymap contains " << mymap.size() << " elements.\n";
return 0;
}
Output:
Example #5
Program to demonstrate the map find() which function somewhat similar with a mere difference to the map at() function.
Code:
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['m']=20;
mymap['n']=40;
mymap['o']=80;
mymap['p']=100;
it = mymap.find('o');
if (it != mymap.end())
mymap.erase (it);
std::cout << "elements in mymap:" << '\n';
std::cout << "m => " << mymap.find('m')->second << '\n';
std::cout << "n => " << mymap.find('n')->second << '\n';
std::cout << "p => " << mymap.find('p')->second << '\n';
return 0;
}
Output:
Conclusion
The Map at() function is a very useful function in C++ as it lets the accessibility of elements and its function return the clear exception or the error describing the fact behind the scene at the time of running the function. It differs a lot from the other operator functions like map operator and map find.
Recommended Articles
This is a guide to C++ map at(). Here we discuss the Syntax and Examples to implement the C++ map at() function and How does it Work. You may also have a look at the following articles to learn more –