C++ map functionMaps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed. For example: A map of Employees where employee ID is the key and name is the value can be represented as: Keys | Values |
---|
101 | Nikita | 102 | Robin | 103 | Deep | 104 | John |
SyntaxParameterkey: The key data type to be stored in the map. type: The data type of value to be stored in the map. compare: A comparison class that takes two arguments of the same type bool and returns a value. This argument is optional and the binary predicate less<"key"> is the default value. alloc: Type of the allocator object. This argument is optional and the default value is allocator. Creating a mapMaps can easily be created using the following statement: The above form will use to create a map with key of type Key type and value of type value type. One important thing is that key of a map and corresponding values are always inserted as a pair, you cannot insert only key or just a value in a map. Example 1Output: Employees[104]=Deep
Map size: 5
Natural Order:
101: Nikita
102: Aman
103: Dolly
104: Deep
105: John
Reverse Order:
105: John
104: Deep
103: Dolly
102: Aman
101: Nikita
Member FunctionsBelow is the list of all member functions of map: Constructor/DestructorFunctions | Description |
---|
constructors | Construct map | destructors | Map destructor | operator= | Copy elements of the map to another map. |
IteratorsFunctions | Description |
---|
begin | Returns an iterator pointing to the first element in the map. | cbegin | Returns a const iterator pointing to the first element in the map. | end | Returns an iterator pointing to the past-end. | cend | Returns a constant iterator pointing to the past-end. | rbegin | Returns a reverse iterator pointing to the end. | rend | Returns a reverse iterator pointing to the beginning. | crbegin | Returns a constant reverse iterator pointing to the end. | crend | Returns a constant reverse iterator pointing to the beginning. |
CapacityFunctions | Description |
---|
empty | Returns true if map is empty. | size | Returns the number of elements in the map. | max_size | Returns the maximum size of the map. |
Element AccessFunctions | Description |
---|
operator[] | Retrieve the element with given key. | at | Retrieve the element with given key. |
ModifiersFunctions | Description |
---|
insert | Insert element in the map. | erase | Erase elements from the map. | swap | Exchange the content of the map. | clear | Delete all the elements of the map. | emplace | Construct and insert the new elements into the map. | emplace_hint | Construct and insert new elements into the map by hint. |
ObserversFunctions | Description |
---|
key_comp | Return a copy of key comparison object. | value_comp | Return a copy of value comparison object. |
OperationsFunctions | Description |
---|
find | Search for an element with given key. | count | Gets the number of elements matching with given key. | lower_bound | Returns an iterator to lower bound. | upper_bound | Returns an iterator to upper bound. | equal_range | Returns the range of elements matches with given key. |
AllocatorFunctions | Description |
---|
get_allocator | Returns an allocator object that is used to construct the map. |
Non-Member Overloaded FunctionsFunctions | Description |
---|
operator== | Checks whether the two maps are equal or not. | operator!= | Checks whether the two maps are equal or not. | operator< | Checks whether the first map is less than other or not. | operator<= | Checks whether the first map is less than or equal to other or not. | operator> | Checks whether the first map is greater than other or not. | operator>= | Checks whether the first map is greater than equal to other or not. | swap() | Exchanges the element of two maps. |
|