当前位置:网站首页>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的安装和配置
![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]

Multi dimensional monitoring: the data base of intelligent monitoring

Based on MCU, how to realize OTA differential upgrade with zero code and no development?

STL教程9-容器元素深拷贝和浅拷贝问题

vulnhub之raven2

ASP.NET-酒店管理系統

Mmc5603nj geomagnetic sensor (Compass example)

Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable

Extrapolated scatter data
随机推荐
2022年湖南工学院ACM集训第二次周测题解
Phpcms prompt message page Jump to showmessage
Uniapp implementation Click to load more
Analysis of EPS electric steering system
How to get started embedded future development direction of embedded
抓包整理外篇fiddler———— 会话栏与过滤器[二]
.\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
Excel表格转到Word中,表格不超边缘纸张范围
Cacti监控Redis实现过程
Redis things
鸿蒙第四次培训
[OBS] encapsulate the basic process of OBS acquisition
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
previous permutation lintcode51
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
Extrapolated scatter data
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
STL教程9-容器元素深拷贝和浅拷贝问题
Arctangent entropy: the latest SCI paper in July 2022
用了这么久线程池,你真的知道如何合理配置线程数吗?