本文共 7518 字,大约阅读时间需要 25 分钟。
c++ stl容器 map
In this tutorial you will learn about STL Map container in C++ i.e., std::map and all functions applicable on it.
在本教程中,您将学习C ++中的STL Map容器,即std :: map及其适用的所有功能。
Map is an associative container. Map satisfies the word “associative”. That means every value in map is associated with a key. All keys are unique. No two mapped values can have same key. The type of key and stored values may differ. All elements follow a strict order. When we insert element automatically stored in its correct position.
地图是一个关联容器。 地图满足“关联”一词。 这意味着映射中的每个值都与一个键相关联。 所有键都是唯一的。 任何两个映射值都不能具有相同的键。 键的类型和存储的值可能不同。 所有元素都遵循严格的顺序。 当我们插入元素时,它会自动存储在其正确位置。
Iterators that can be applicable on map:
可以在地图上应用的迭代器:
begin(): returns iterator to the beginning.
begin():将迭代器返回到开头。
end(): returns iterator to the end of the map.
end():将迭代器返回到地图的末尾。
rbegin(): returns reverse iterator to reverse beginning.
rbegin():将反向迭代器返回为反向开始。
rend(): returns reverse iterator to reverse end.
rend():将反向迭代器返回到反向端点。
cbegin(): Returns constant iterator to the beginning.
cbegin():将常数迭代器返回到开头。
cend(): Returns constant iterator to the end.
cend():将常量迭代器返回到末尾。
These iterators we can use in our programs.
我们可以在程序中使用这些迭代器。
First thing we need to include map header file. Which is #include<map>
首先,我们需要包含地图头文件。 这是#include <map>
Let see some functions associated with map:
让我们看看一些与map相关的功能:
Inserting element into map:
将元素插入地图:
Whenever an element inserted into map, size of the map increases. Since map contains unique keys when we try to insert an element into map, it always checks whether any element already inserted or not with this same key value.
每当将元素插入地图时,地图的大小都会增加。 由于在我们尝试将元素插入地图时map包含唯一键,因此它始终检查是否已使用相同的键值插入任何元素。
There are different ways we can insert elements into map.
我们可以通过多种方式将元素插入地图。
Method 1: Insert directly by passing element and its corresponding key.
方法1:通过传递元素及其相应的键直接插入。
mapName.insert (pair <keydataType, value dataType> ( key, value ));
mapName.insert(对<keydataType,value dataType>(key,value));
Method 2: Using iterator. This returns iterator at inserted position.
方法2:使用迭代器。 这将在插入位置返回迭代器。
mapName.insert (iterator, pair < keyDatatype, value datatype > (key value, data value));
mapName.insert(迭代器,配对<keyDatatype,值datatype>(键值,数据值));
Example program for inserting into map:
用于插入地图的示例程序:
#include#include
Output
输出量
0 0 1 10 2 20 3 30 4 40 10 10 99 99
0 0 1 10 2 20 3 30 4 40 10 10 99 99
Other modification functions on map:
地图上的其他修改功能:
erase(): We can remove single element or a range of elements. The effect on map is whenever a value deleted from map, its size get reduced.
delete():我们可以删除单个元素或一系列元素。 对地图的影响是每当从地图中删除一个值时,其大小就会减小。
swap(): swaps elements of map1 to map2 and map2 to map1. Here both map1 and map2 are need not be of same size.
swap():将map1的元素交换为map2,将map2的元素交换为map1。 这里map1和map2的大小不必相同。
clear(): removes all elements in the map. It results map of size 0.
clear():删除地图中的所有元素。 结果地图大小为0。
Example program to show above function:
显示上述功能的示例程序:
#include#include
Output
输出量
printing remaining elements after one erase operation 1 11 2 12 3 13 4 14 checking map empty or not map is empty map1 elements before swap 0 10 1 11 2 12 3 13 4 14 map2 elements before swap 0 0 1 10 2 20 3 30 4 40 map1 elements after swap 0 0 1 10 2 20 3 30 4 40 map2 elements after swap 0 10 1 11 2 12 3 13 4 14 applying clear operation map is empty by clear operation
一次擦除操作后打印剩余元素 1 11 2 12 3 13 4 14 检查映射为空或 映射是否为空 交换前的map1元素 0 10 1 11 2 12 3 13 4 14 交换前的map2元素 0 0 1 10 2 20 3 30 4 交换后 40个 map1元素 0 0 1 10 2 20 3 30 4 交换后 40个 map2元素 0 10 1 11 2 2 3 3 13 4 14 应用清除操作清除操作将 映射为空
Functions related to capacity:
与容量有关的功能:
empty(): returns a Boolean value whether map is empty or not.
empty():无论地图是否为空,都会返回一个布尔值。
size(): returns the size of the map.
size():返回地图的大小。
max_size(): returns the maximum size a map can have.
max_size():返回地图可以具有的最大尺寸。
Example program to show above functions:
显示上述功能的示例程序:
#include#include
Output
输出量
map contains some elements size of the map is 5 maximum size of the map is 461168601842738790
地图包含一些元素 ,地图的大小是5,地图的 最大大小是461168601842738790
Comment below if you have queries or found any information incorrect in above tutorial for C++ STL Map container i.e. std::map.
如果您有疑问或在上面的教程中对C ++ STL Map容器(例如std :: map)有任何不正确的信息,请在下面评论。
翻译自:
c++ stl容器 map
转载地址:http://yqggb.baihongyu.com/