📅  最后修改于: 2020-10-18 03:34:36             🧑  作者: Mango
映射是C++ STL(标准模板库)的一部分。映射是存储排序的键值对的关联容器,其中每个键都是唯一的,可以插入或删除但不能更改。与键关联的值可以更改。
例如:一张员工图,其中员工ID是键,名称是值,可以表示为:
Keys | Values |
---|---|
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 > // map::allocator_type
> class map;
key:要存储在地图中的密钥数据类型。
type:要存储在地图中的值的数据类型。
compare:一个比较类,它接受两个bool类型相同的参数并返回一个值。此参数是可选的,二进制谓词less <“ key”>是默认值。
alloc:分配器对象的类型。此参数是可选的,默认值为allocator。
使用以下语句可以轻松创建地图:
typedef pair value_type;
上面的表格将用于创建一个键类型为Key type且值类型为value value类型的映射。重要的一点是,映射的键和相应的值始终成对插入,不能在映射中仅插入键或仅插入值。
#include
#include
#include
输出:
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
以下是map的所有成员函数的列表:
Functions | Description |
---|---|
constructors | Construct map |
destructors | Map destructor |
operator= | Copy elements of the map to another map. |
Functions | 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. |
Functions | 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. |
Functions | Description |
---|---|
operator[] | Retrieve the element with given key. |
at | Retrieve the element with given key. |
Functions | 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. |
Functions | Description |
---|---|
key_comp | Return a copy of key comparison object. |
value_comp | Return a copy of value comparison object. |
Functions | 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. |
Functions | Description |
---|---|
get_allocator | Returns an allocator object that is used to construct the map. |
Functions | 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. |