当前位置:网站首页>STL tutorial 8-map
STL tutorial 8-map
2022-07-03 11:45:00 【Sleepy snail】
map
map be relative to set difference ,map With key value and real value , All elements are automatically sorted according to the key value .pair The first element of is called the key value , The second element is called a real value .map It also takes the red black tree as the underlying implementation mechanism .
map Four insertion methods
map<int, int> mymap;// Statement map, The first parameter key type , The second parameter value type
mymap.insert(pair<int, int>(10, 10));// The first way to insert
mymap.insert(make_pair(20, 20));// The second insertion method
mymap.insert(map<int, int>::value_type(30, 30));// The third kind of
mymap[40] = 40;// A fourth
// Print
for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it What comes out is pair
cout << "key:" << (*it).first << "value:" << it->second << endl;
}
Note the fourth insertion method above , If no access is inserted , such as cout Print mymap[40], At this time, it will also be inserted , Because the value type is int, So... Will be inserted 0
Add custom type
Add a class here
class MyKey
{
public:
MyKey(int index, int id)
{
this->mIndex = index;
this->mID = id;
}
public:
int mIndex;
int mID;
};
Define comparison type
struct mycompare
{
bool operator()(const MyKey& key1, const MyKey& key2)const
{
return key1.mIndex > key2.mIndex;
}
};
Additive elements
map<MyKey, int, mycompare> mymap;// Automatic sorting , Tell him how to sort
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
Print elements
for (auto it = mymap.begin(); it != mymap.end(); it++)
{
cout << it->first.mIndex << ":" << it->first.mID << "=" << it->second << endl;
}
边栏推荐
- Kibana~Kibana的安装和配置
- 聊聊Flink框架中的状态管理机制
- How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
- The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
- Go语言实现静态服务器
- Double linked list of linear list
- 鸿蒙第四次培训
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
- C language AES encryption and decryption
- After using the thread pool for so long, do you really know how to reasonably configure the number of threads?
猜你喜欢
![[OBS] configFile in ini format of OBS](/img/b2/0b130cee6ea884557a30e4b408f49e.png)
[OBS] configFile in ini format of OBS

Hongmeng third training (project training)

错排问题 (抽奖,发邮件)

Mmc5603nj geomagnetic sensor (Compass example)
![Capturing and sorting out external Fiddler -- Conversation bar and filter [2]](/img/04/e9cc027d753e7049f273d866eefdce.png)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.

Numpy np. Max and np Maximum implements the relu function

ASP.NET-酒店管理系統

一文搞懂Go语言Context

Application of high-precision indoor positioning technology in safety management of smart factory
随机推荐
Qt+VTK+OCCT读取IGES/STEP模型
Nestjs配置服务,配置Cookie和Session
PHP基础
CSRF
FL Studio 20 unlimited trial fruit arranger Download
Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
JGG专刊征稿:时空组学
2022年中南大学夏令营面试经验
C language AES encryption and decryption
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
libvirt 中体验容器
Numpy np. Max and np Maximum implements the relu function
Internet socket (non) blocking write/read n bytes
P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
Nestjs configuration service, configuring cookies and sessions
Keepalived中Master和Backup角色选举策略
《剑指offer 04》二维数组查找
Viewing binary bin files with notepad++ editor
Cadence background color setting