Updated April 1, 2023
Introduction to C++ Map
The Maps are containers that store key-value pair elements in sorted form. The Map is a built-in class in the C++ standard template library. The Map properties are it store elements in sorted form based on the keys, it stores unique keys that can be added or removed but cannot be updated and values corresponding with keys can be duplicated and can be updated. The values can be accessed from the map through the keys themselves. To use the maps in a program we use #include <map> header file.
Syntax of Map in C++
template < class Key, class T, class Compare = less<Key>, class Alloc = allocator <pair <const Key,T> > > class map;
where –
class Key is map::key_type
class T is map::mapped_type
class Compare = less<Key> is map::key_compare
class Alloc = allocator <pair <const Key,T> > is map::allocator_type
Parameters
- Key -Key specifies the data type of the keys.
- T – Tspecifies the data type of the values.
- Compare –compare specifies the comparison class which is used for comparison and it accepts two-parameter of the same type and returns a bool value true or false based on the comparison. It is optional and its default value is the binary predicate less<“key”>.
- Alloc -Alloc specifies an allocator object, which is optional and the default is allocator value.
How to Create a map in C++?
Consider an example where we create a map of Students where Student ID is the key and their mark is the value can be represented as { 101 : 60, 102 : 68, 103 : 60, 104 : 90, 105 : 88}, as here all keys are unique and values can be duplicates. Note that key and their associated values should be inserted in a pair of a map, we cannot insert key or value alone in a map.
Examples of map class in c++
Next we write the c++ code to understand the map more clearly with the following example where we use map class to store the student’s id and their respective marks, as below –
Code:
#include <iostream>
#include <string.h>
#include <utility>
#include <map>
using namespace std;
int main()
{
map <int, float> Students;
// Store key value pair elements in map student
Students[101] = 50;
Students[104] = 90;
Students[101] = 60; // duplicate key
Students[103] = 60; // duplicate value
Students[105] = 88;
Students[102] = 68;
// access value by key in map
cout << "Students[101]=" << Students[101] << endl ;
cout << "Students[104]=" << Students[104] << endl << endl;
cout << "Size of the Map is = " << Students.size() << endl;
cout << "The Store order of the Map is =" << endl;
// iterate elements of student map
for( map<int, float>::iterator i= Students.begin(); i != Students.end(); i++)
{
cout << (*i).first << ": " << (*i).second << endl;
}
return 0;
}
Output:
As in the above code, the map class is used to create a map of students where it stores the key as their id and values as their marks. Here the key data type is an integer and the value data type is float as specified by line map <int, float> Students;. As we can see in the code the values of the student’s map are accessed by using the keys, it internally stored the elements in order of the keys, when the duplicate keys are passed it store only unique key but with the updated or latest value and when the duplicate values are passed it accept and store duplicate values.
C++ Map Member Functions
Let’s see the list of all map’s member functions category wise which can be used for the specific purposes for a map –
1. Member function
- Destructors – Map destructor, which is public
- Constructors – Construct map, which is public
- operator= – Copy container elements, which is public
2. Iterators
- begin – It gives an iterator to the beginning (first element of the map).
- end – It gives an iterator to end (past-end of the map).
- rbegin – It gives a reverse iterator to the last element of the map.
- rend – It gives a reverse iterator to the first element of the map.
- cbegin – It gives a const_iterator to begning (first element of the map).
- cend – It gives a const_iterator to end (past-end of the map).
- crbegin – It gives a const_reverse iterator to the last element of the map.
- crend – It gives a const_reverse iterator to the first element of the map.
3. Capacity
- size – It gives the size(number of elements) of the map.
- empty – It gives true when the map is empty.
- max_size – It gives the max size of the map.
4. Element Access
- at – It is used to access the element by key.
- operator[ ] – It is used to access the element by key.
5. Modifiers
- clear – It used to delete all the elements.
- erase – It is used to erase elements.
- insert – It is used to insert an element.
- emplace – It is used to create and insert a new element.
- emplace_hint – It is used to create and insert new elements by hint.
- swap – It is used to exchange the element.
6. Observers
- key_comp – It returns the comparison object of the key.
- value_comp – It returns the comparison object of value.
7. Operations
- count – It returns the count of elements with the specified key.
- find – It finds an element by a given key.
- lower_bound – It gives lower bound iterator.
- upper_bound- It gives an upper bound iterator.
- equal_range – It gives the range iterator with a specified key.
8. Allocator
- get_allocator – It Returns an object of allocator which is used to create a map.
9. Overloaded Non-Member Functions
- operator< – It overloads to determine whether the first map is lesser than the second or not.
- operator> – It overload to determine whether the first map is greater than the second or not.
- operator== – It overloads to determine whether two maps are equal or not.
- operator!= – It overloads to determine whether two maps are not equal or not.
- operator<= – It overload to determine whether the first map is lesser than an equal second or not.
- operator>= – It overload to determine whether the first map is greater than an equal second or not.
Conclusion
The Map class is a built-in class in the C++ Standard Template Library which acts as a container to store key-value pair elements in sorted form. The time complexity of a map for insert, delete and search is O(n).
Recommended Articles
This is a guide to C++ Map. Here we discuss the Examples for the map class in C++ and How to Create it along with the parameters and syntax. You may also have a look at the following articles to learn more –