English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
map is C ++ Part of STL (Standard Template Library). map is an associative container that stores sorted key-value pairs, where each key is unique, can be inserted or deleted, but cannot be changed. However, the value associated with the key can be changed.
For example:An employee map container where the employee ID is the key and the name is the value, can be represented as:
Key | Value |
---|---|
101 | Nikita |
102 | Robin |
103 | Deep |
104 | John |
template < class Key,</ //map::key_type class T,</ //map::mapped_type class Compare = less,</ //map::key_compare class Alloc = allocator<pair> //map::allocator_type > class map;
key:The data type of the key to be stored in the map.
type:The data type of the value to be stored in the map.
compare:A comparison class that accepts two bool type parameters and returns a value. This parameter is optional, and the binary predicate less <" key"> is the default value.
alloc:The type of the allocator object. This parameter is optional, and the default value is the allocator
You can easily create a map with the following statement:
typedef pair<const Key, T> value_type;
The following statement will be used to create a key type ofKey type,andvaluetypeis of typeThe map.An important point is that the keys and corresponding values in the map are always inserted in pairs, you cannot insert only keys or only values in the map.
#include <string.h> #include <iostream> #include <map> #include <utility> using namespace std; int main() { mapEmployees; // 1) using array index notation for assignment Employees[101] = "Nikita"; Employees[105] = "John"; Employees[103] = "Dolly"; Employees[104] = "Deep"; Employees[102] = "Aman"; cout << "Employees[104] = << Employees[104] << endl << endl; cout << "Map size: " << Employees.size() << endl; cout << endl << "Natural order:" << endl; for (map::iterator ii = Employees.begin(); ii != Employees.end(); ++ii) { cout << (*ii).first << ": " << (*ii).second << endl; } cout << endl << "Reverse order:" << endl; for(map::reverse_iterator ii=Employees.rbegin(); ii!=Employees.rend(); ++ii) { cout << (*ii).first << ": " << (*ii).second << endl; } }
Output:
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
The following is a list of all member functions of map:
Function | Description |
---|---|
constructors | Construct map |
destructors | Map destructor |
operator= | Copy the elements of the map to another map container. |
Function | Description |
---|---|
begin | Return an iterator pointing to the first element in the map. |
cbegin | Return a const iterator pointing to the first element in the map. |
end | Return an iterator pointing to the end. |
cend | Return a constant iterator pointing to the end. |
rbegin | Return a reverse iterator pointing to the end. |
rend | Return a reverse iterator pointing to the beginning. |
crbegin | Return a constant reverse iterator pointing to the end. |
crend | Return a constant reverse iterator pointing to the beginning. |
Function | Description |
---|---|
empty | Return true if the map is empty. |
size | Return the number of elements in the map. |
max_size | Return the maximum capacity of the map. |
Function | Description |
---|---|
operator[] | Retrieve the element with the given key. |
at | Retrieve the element with the given key. |
Function | Description |
---|---|
insert | Insert elements into the map. |
erase | Erase elements from the map. |
swap | Swap the contents of the map. |
clear | Delete all elements of the map. |
emplace | Construct a new element and insert it into the map. |
emplace_hint | Construct a new element through hint and insert it into the map. |
Function | Description |
---|---|
key_comp | Return a copy of the key comparison object. |
value_comp | Return a copy of the value comparison object. |
Function | Description |
---|---|
find | Search for an element with the given key. |
count | Get the number of elements that match the given key. |
lower_bound | Return the lower bound of the iterator. |
upper_bound | Return an iterator to the upper bound. |
equal_range | Return the range of elements that match the given key. |
Function | Description |
---|---|
get_allocator | Return the allocator object used to construct the map. |
Function | Description |
---|---|
operator== | Check if two maps are equal. |
operator!= | Check if two maps are equal. |
operator< | Check if the first map is less than other maps. |
operator<= | Check if the first map is less than or equal to other maps. |
operator> | Check if the first map is greater than other maps. |
operator>= | Check if the first map is greater than other maps. |
swap() | Swap elements of two maps. |