当前位置:网站首页>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;
}
边栏推荐
- libvirt 中体验容器
- PHP Basics
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
- 简单工厂和工厂方法模式
- ASP. Net hotel management system
- Linear table sequence table comprehensive application problem p18
- 剑指offer专项32-96题做题笔记
- mysql使用update联表更新的方法
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
猜你喜欢

Groovy测试类 和 Junit测试

Slam mapping and autonomous navigation simulation based on turnlebot3

牛牛的组队竞赛

vulnhub之cereal

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

鸿蒙第三次培训(项目实训)

(database authorization - redis) summary of unauthorized access vulnerabilities in redis

Numpy np. Max and np Maximum implements the relu function

机器学习 3.2 决策树模型 学习笔记(待补)

PHP server interacts with redis with a large number of close_ Wait analysis
随机推荐
STL教程10-容器共性和使用场景
程序员的创业陷阱:接私活
How to mix embedded MCU, arm and DSP?
Phpcms prompt message page Jump to showmessage
MySQL union和union all区别
cgroup简介
Nestjs configuration service, configuring cookies and sessions
PHP server interacts with redis with a large number of close_ Wait analysis
剑指offer专项32-96题做题笔记
聊聊Flink框架中的状态管理机制
The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
[OBS] encapsulate the basic process of OBS acquisition
在CoreOS下部署WordPress实例教程
typeScript
Asyncio warning deprecationwarning: there is no current event loop
Excel表格转到Word中,表格不超边缘纸张范围
按键切换:按F1-F12都需要按Fn
鸿蒙第三次培训(项目实训)
(数据库提权——Redis)Redis未授权访问漏洞总结
用了这么久线程池,你真的知道如何合理配置线程数吗?