当前位置:网站首页>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;
}
边栏推荐
- The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
- Using onvif protocol to operate the device
- STL教程10-容器共性和使用场景
- 软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
- 银泰百货点燃城市“夜经济”
- 软考中级软件设计师该怎么备考
- Unity3D学习笔记5——创建子Mesh
- Kibana - installation and configuration of kibana
- C语言 AES加解密
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
猜你喜欢
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
Hongmeng fourth training
vulnhub之cereal
机器学习 3.2 决策树模型 学习笔记(待补)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
C语言 AES加解密
Application of high-precision indoor positioning technology in safety management of smart factory
2022 northeast four provinces match VP record / supplementary questions
The excel table is transferred to word, and the table does not exceed the edge paper range
Analysis of EPS electric steering system
随机推荐
Dynamic programming (interval DP)
Uniapp implementation Click to load more
CSRF
错排问题 (抽奖,发邮件)
836. 合并集合(DAY 63)并查集
Excel快速跨表复制粘贴
Unity3D学习笔记5——创建子Mesh
同事写了一个责任链模式,bug无数...
Oracle withdraw permission & create role
聊聊Flink框架中的状态管理机制
Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
2022 东北四省赛 VP记录/补题
phpcms 提示信息页面跳转showmessage
Understand go language context in one article
抓包整理外篇fiddler———— 会话栏与过滤器[二]
[OBS] encapsulate the basic process of OBS acquisition
How should intermediate software designers prepare for the soft test
并发编程-单例
Double linked list of linear list