The C++ map operator function enables accessing elements in the map using a specified key value.
It resembles the at function in functionality. The key distinction lies in the fact that while at throws an exception when the accessed key is absent in the map, the operator adds the key to the map if it is not already present.
Syntax
Consider the key value k , syntax would be:
mapped_type& operator[] (const key_type& k); //until C++ 11
mapped_type& operator[] (const key_type& k); //since C++ 11
mapped_type& operator[] (key_type&& k); //since C++ 11
Parameter
k : The key value of the element from which the corresponding mapped value is retrieved.
Return value
It provides a pointer to the mapped value associated with the key in the element.
Example 1
Let's review a basic example for accessing the elements.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
cout << "Map contains following elements" << endl;
cout << "m['a'] = " << m['a'] << endl;
cout << "m['b'] = " << m['b'] << endl;
cout << "m['c'] = " << m['c'] << endl;
cout << "m['d'] = " << m['d'] << endl;
cout << "m['e'] = " << m['e'] << endl;
return 0;
}
Output:
Map contains following elements
m['a'] = 1
m['b'] = 2
m['c'] = 3
m['d'] = 4
m['e'] = 5
In the previous example, the operator method is employed to retrieve elements from a map data structure.
Example 2
Let's observe a basic illustration of adding elements based on their key values.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<int,string> mymap = {
{ 101, "" },
{ 102, "" },
{ 103, ""} };
mymap[101] = "Java";
mymap[102] = "T";
mymap[103] = "Point";
// prints value associated with key 101, i.e. Java
cout<<mymap[101];
// prints value associated with key 102, i.e T
cout<<mymap[102];
// prints value associated with key 103, i.e Point
cout<<mymap[103];
return 0;
}
Output:
Cpp Tutorial
In the example provided, the operator is employed to insert elements following initialization using the corresponding key values.
Example 3
Let's examine a basic illustration of modifying the value linked to a specific key.
#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[100] = "Nidhi"; // changes the value associated with key 100 to Nidhi
mymap[300] = "Pinku"; // changes the value associated with key 300 to Pinku
mymap[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 aforementioned example, the operator method is employed to modify the values linked to their respective keys.
Example 4
Let's examine a basic illustration to distinguish between operator and at.
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
map<char,string> mp = {
{ 'a',"Java"},
{ 'b', "C++" },
{ 'c', "Python" }};
cout<<endl<<mp['a'];
cout<<endl<<mp['b'];
cout<<endl<<mp['c'];
mp['d'] = "SQL";
/* since there is no key with value 'd' in the map,
it insert a key-value pair in map with key 'd' and value = "SQL" */
cout<<endl<<mp['d'];
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<<"\nOut of Range Exception at "<<e.what();
}
return 0;
}
Output:
Java
C++
Python
SQL
Out of Range Exception at map::at
When utilizing the at function in the aforementioned scenario, it triggers an outofrange Exception due to the absence of a key with the value 'z' in the map. Conversely, employing the operator and appending an element in the key 'd' results in the insertion of a key-value pair in the map with 'd' as the key and "SQL" as the value.