Maps are containers in STL that stores the elements as key-value pairs. Each element has a unique key value and the elements are stored in sorted order based on their key value. Keys cannot be changed, once they are added to the map, they can only be inserted or deleted, but the values can be altered. They are implemented as Red-Black Trees in memory. The map is present in #include<map> header file. The elements inside the map can be accessed using iterators.
Map Declaration
Syntax:
map < key_data_type, value_data_type > name { initial_values };
key_data_type: data type of the key to be stored inside the map
value_data_type: data type of the value to be stored inside the map
initial_values: optional parameter which initializes the map with the given values
Note: By default the map stores values in non-decreasing order sorted according to key. To store the values in non-increasing order, we use an inbuilt comparator function
map < key_data_type, value_data_type, greater < data_type >> name;
Example:
map < int, string > m; //initializes a map of size 0 which have key elements as integers and values as strings arranged in non-decreasing order based on keys
map < int, string > m = { { 1, "a" }, { 2, "ab" }, { 3, "abc" } //initializes a map having initial key-value pairs as {1, a}, {2, ab}, {3, abc}
map < int, string, greater < int >> m; //initializes a map of size 0 which which have key elements as integers and values as strings arranged in non-increasing orderFunctions on maps
begin(): Returns an iterator to the first element of the map.
Parameters: None
Return type: iterator
end(): Returns an iterator to the element past the last element of the map.
Parameters: None
Return type: iterator
[key]: Returns the value associated with the given key.
Parameters: Key
Return type: value, associated with the key
size(): It tells us the size of the map.
Parameters: None
Return type: integer - total number of elements in the map
insert(pair): Insert a pair in the map.
Parameters: the pair to be inserted
Return type: void
erase(key)orerase(pos_iterator): Delete an element from the map.
Parameters: the key of the element to be removed or iterator pointing to the position at which the element needs to be deleted
Return type: void
find(key): Returns an iterator pointing to the element, if the key is found else returns an iterator pointing to the end of the map.
Parameters: the element which needs to be found
Return type: iterator
clear(): It deletes all the elements from the map
Parameters: None
Return type: void
empty(): It tells us whether the map is empty or not.
Parameters: None
Return type: Boolean, true if a map is empty else false
#include<iostream>
#include<map>
using namespace std;
int main() {
map < int, string > m1;
m1.insert(pair < int, string > (1, "a"));
map < int, string, greater < int >> m2;
m2.insert(pair < int, string > (1, "a"));
m1[2] = "ab";
m1[3] = "abc";
m1[4] = "abcd";
m2[2] = "ab";
m2[3] = "abc";
m2[4] = "abcd";
map < int, string > ::iterator it;
cout << "Map1\n";
for (it = m1.begin(); it != m1.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
cout << "Map2\n";
for (it = m2.begin(); it != m2.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
m1.erase(1);
m2.erase(m2.find(1));
cout << "After erasing element, size of map1 is " << m1.size() << '\n';
cout << "After erasing element, size of map2 is " << m2.size() << '\n';
int val = 3;
if (m1.find(val) != m1.end())
cout << "The map1 contains " << val << " as key" << endl;
else
cout << "The map1 does not contains " << val << " as key" << endl;
cout << "Elements of map1\n";
for (it = m1.begin(); it != m1.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
cout << "Elements of map2\n";
for (it = m2.begin(); it != m2.end(); it++)
cout << it -> first << " " << it -> second << '\n';
cout << '\n';
m1.clear();
if (m1.empty() == true) {
cout << "Map1 is empty now!";
}
return 0;
}Output
Map1
1 a
2 ab
3 abc
4 abcd
Map2
4 abcd
3 abc
2 ab
1 a
After erasing element, size of map1 is 3
After erasing element, size of map2 is 3
The map1 contains 3 as key
Elements of map1
2 ab
3 abc
4 abcd
Elements of map2
4 abcd
3 abc
2 ab
Map1 is empty now!