博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ stl容器 map_C ++ STL映射容器– std :: map
阅读量:2510 次
发布时间:2019-05-11

本文共 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.

地图是一个关联容器。 地图满足“关联”一词。 这意味着映射中的每个值都与一个键相关联。 所有键都是唯一的。 任何两个映射值都不能具有相同的键。 键的类型和存储的值可能不同。 所有元素都遵循严格的顺序。 当我们插入元素时,它会自动存储在其正确位置。

C ++ STL映射 (C++ STL Map)

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
#include
 using namespace std; int main(){ map < int, int> mp1; // declaring map map < int, int> :: iterator it; // Inserting method 1: for (int i=0; i<5; i++) { mp1.insert (pair < int, int> (i, i*10)); } // Caution: If you give any constant in place of first argument in insert. Only the first value will be inserted. Remaining will be pushed out as duplicates. // inserting method 2: it= mp1.begin(); mp1.insert(it, pair < int, int> (99, 99)); // Even this was inserted at starting position, due to porperty of ordering of associative containers it sorted according to its key value and inserted at last position. it= mp1.end(); mp1.insert(it, pair < int, int> (10, 10)); // printing for ( it= mp1.begin(); it!=mp1.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } return 0;}

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
#include
 using namespace std; int main(){ map < int, int > mp1; map < int, int > mp2; map
:: iterator it; for (int i=0; i<5; i++){ // inserting elements into map mp1.insert (pair
(i,i+10)); } // deleting element pointed by iterator it = mp1.begin(); mp1.erase(it); // key 0 deleted cout << "printing remaining elements after one erase operation" << endl; for ( it= mp1.begin(); it!=mp1.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } // erasing range of values it= mp1.begin(); mp1.erase(it,mp1.end()); // this will erase all map cout << "checking map empty or not" << endl; int chk= mp1.empty(); if (chk==1 ) cout << "map is empty" << endl; else cout << "map is not empty" << endl; for (int i=0; i<5; i++){ // inserting elements into map1 mp1.insert (pair
(i,i+10)); } for (int i=0; i<5; i++){ // inserting elements into map2 mp2.insert (pair
(i,i*10)); } cout << "map1 elements before swap" << endl; for ( it= mp1.begin(); it!=mp1.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } cout << "map2 elements before swap" << endl; for ( it= mp2.begin(); it!=mp2.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } // doing swaping operation mp1.swap(mp2); cout << "map1 elements after swap" << endl; for ( it= mp1.begin(); it!=mp1.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } cout << "map2 elements after swap" << endl; for ( it= mp2.begin(); it!=mp2.end(); it++) { cout << it -> first; cout << '\t'; cout << it -> second; cout << endl; } cout << "applying clear operation" << endl; mp1.clear(); mp2.clear(); chk= mp1.empty(); if (chk == 1) cout << "map is empty by clear operation" << endl; else cout << "map is not empty"; return 0;}

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
#include
 using namespace std; int main(){ map < int, int> mp1; map < int, int> :: iterator it; for(int i=0; i<5; i++) { mp1.insert (pair
(i, i*10 )); } int chk = mp1.empty(); if (chk == 1) cout << " map is empty" << endl; else cout << "map contains some elements" << endl; cout << "size of the map is " ; cout << mp1.size() << endl; cout << "maximum size of the map is " ; cout << mp1.max_size() << endl; return 0;}

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/

你可能感兴趣的文章
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
课堂练习之买书打折最便宜
查看>>
定义函数
查看>>
网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP
查看>>
MQTT协议笔记之mqtt.io项目HTTP协议支持
查看>>
(转)jQuery中append(),prepend()与after(),before()的区别
查看>>
Tecplot: Legend和图像中 Dashed/Dash dot/Long dash 等虚线显示没有区别的问题
查看>>
蜕变成蝶~Linux设备驱动之异步通知和异步I/O
查看>>
jquery简单开始
查看>>
作业2
查看>>
ios上架报错90080,90087,90209,90125 解决办法
查看>>
给button添加UAC的小盾牌图标
查看>>
如何退出 vim
查看>>
Robberies
查看>>
get post 提交
查看>>
R安装
查看>>
JavaScript高级特性-实现继承的七种方式
查看>>