Maps in CPP are used to store sorted key-value pair. They are the associative containers. Each key in a map is unique. CPP facilitates insertion and deletion of a key in a map but do not allow any modifications, however, values can be modified.
Member Functions of a CPP map:
Allocator:
| FUNCTION | USES |
| get_allocator | To return an allocator object to construct the map. |
Capacity:
| FUNCTION | USES |
| empty() | To determine whether the map is empty or not. |
| max_size() | To determine the maximum size of the map. |
| size() | To determine number of elements in the map. |
Constructor/Destructor:
| FUNCTION | USES |
| (constructor) | To construct a map. |
| (destructor) | To destruct a map. |
| operator= | To copy the elements of the map to another map. |
Element Access
| FUNCTION | USES |
| at | To get the element with given key. |
| operator[] | To get the element with given key. |
Iterators:
| FUNCTION | USES |
| begin() | To point to the first element of the map. |
| cbegin() | To return a constant iterator to the beginning of the map. |
| cend() | To return a constant iterator to the end. |
| crbegin() | To return a const reverse iterator to the beginning. |
| crend() | To return a const reverse iterator to the end. |
| end() | To return an iterator to the end. |
| rbegin() | To return a reverse iterator to the beginning. |
| rend() | To return a reverse iterator to the end. |
Modifiers:
| FUNCTION | USES |
| clear() | To remove all the elements of the map. |
| erase() | To delete the specified element. |
| emplace() | To construct and insert a new element at a specified position. |
| emplace_hint() | To construct and insert a new element at a specified position by hint. |
| insert() | To insert a new element in the map.. |
| swap() | To exchange the contents of two maps. |
Non-Member Overloaded Functions:
| FUNCTION | USES |
| operator== | To determine whether the two maps are equal or not. |
| operator!= | To determine whether the two maps are equal or not. |
| operator<= | To determine whether the first map is less than or equal to other or not. |
| operator< | To determine whether the first map is less than other or not. |
| operator>= | To determine whether the first map is greater than equal to other or not. |
| operator> | To determine whether the first map is greater than other or not. |
| swap() | To exchange the contents of two maps. |
Observers:
| FUNCTION | USES |
| key_comp | To return a copy of key comparison object. |
| value_comp | To return a copy of value comparison object. |
Operations:
| FUNCTION | USES |
| count | To get the number of elements matching with given key. |
| equal_range | To get the range of elements matches with given key. |
| find | To find an element with given key. |
| lower_bound | To get an iterator to lower bound. |
| upper_bound | To get an iterator to upper bound. |
Example:
#include <string.h> #include <iostream> #include <map> #include <utility> using namespace std; int main() { map<int, string> Students; Students[1] = "Vibhu"; Students[2] = "Yash"; Students[3] = "Abhi"; Students[4] = "Neelam"; cout << "Students[2]=" << Students[2] << "\n\n"; cout << "Map size: " << Students.size() << endl; cout << "\nOrder:" << endl; for( map<int,string>::iterator i=Students.begin(); i!=Students.end(); ++i) { cout << (*i).first << ": " << (*i).second << endl; } cout << "\nReverse Order:" << endl; for( map<int,string>::reverse_iterator i=Students.rbegin(); i!=Students.rend(); ++i) { cout << (*i).first << ": " << (*i).second << endl; } } |
Output
Students[2]=Yash Map size: 4 Order: 1: Vibhu 2: Yash 3: Abhi 4: Neelam Reverse Order: 4: Neelam 3: Abhi 2: Yash 1: Vibhu |