📜  C++ STL-map

📅  最后修改于: 2020-10-18 03:34:36             🧑  作者: Mango

C++映射函数

映射是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类型的映射。重要的一点是,映射的键和相应的值始终成对插入,不能在映射中仅插入键或仅插入值。

例子1

#include 
#include 
#include 
#include 
using namespace std;
int main()
{
   map Employees;
   // 1) Assignment using array index notation
   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;
   }
}

输出:

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.