当前位置:网站首页>STL教程8-map
STL教程8-map
2022-07-03 10:45:00 【贪睡的蜗牛】
map
map相对于set区别,map具有键值和实值,所有元素根据键值自动排序。pair的第一元素被称为键值,第二元素被称为实值。map也是以红黑树为底层实现机制。
map四种插入方式
map<int, int> mymap;//声明map,第一个参数key类型,第二个参数value类型
mymap.insert(pair<int, int>(10, 10));//第一种插入方式
mymap.insert(make_pair(20, 20));//第二种插入方式
mymap.insert(map<int, int>::value_type(30, 30));//第三种
mymap[40] = 40;//第四种
//打印
for (map<int, int>::iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it取出来的是pair
cout << "key:" << (*it).first << "value:" << it->second << endl;
}
注意上面第四种插入方式,如果没有插入访问,比如cout打印mymap[40],这时候也会插入,由于值类型是int,因此会插入0
添加自定义类型
这里添加一个类
class MyKey
{
public:
MyKey(int index, int id)
{
this->mIndex = index;
this->mID = id;
}
public:
int mIndex;
int mID;
};
定义比较类型
struct mycompare
{
bool operator()(const MyKey& key1, const MyKey& key2)const
{
return key1.mIndex > key2.mIndex;
}
};
添加元素
map<MyKey, int, mycompare> mymap;//自动排序,要告诉他怎么排序
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
打印元素
for (auto it = mymap.begin(); it != mymap.end(); it++)
{
cout << it->first.mIndex << ":" << it->first.mID << "=" << it->second << endl;
}
边栏推荐
- .\vmware-vdiskmanager.exe -k “c:\\xxxxx.vmdk”
- ASP. Net hotel management system
- 多维度监控:智能监控的数据基础
- 2022 northeast four provinces match VP record / supplementary questions
- After a month, I finally got Kingdee offer! Share tetrahedral Sutra + review materials
- Reading notes: heart like Bodhi, Cao Dewang
- Spl06-007 air pressure sensor (example of barometer)
- 软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
- C language AES encryption and decryption
- ASP.NET-酒店管理系统
猜你喜欢

Excel quick cross table copy and paste

ASP. Net hotel management system

(2) Base

高精度室内定位技术,在智慧工厂安全管理的应用

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

2022 northeast four provinces match VP record / supplementary questions

Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3

AMS Series 1 - AMS startup process

Excel表格转到Word中,表格不超边缘纸张范围

Incremental database backup - DB incr DB full
随机推荐
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
[VTK] vtkWindowedSincPolyDataFilter 源码注释解读
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
程序员的创业陷阱:接私活
Arctangent entropy: the latest SCI paper in July 2022
Modular programming of single chip microcomputer
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
2. Hal hardware abstraction layer
C language AES encryption and decryption
Web安全总结
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
Oracle withdraw permission & create role
软考中级软件设计师该怎么备考
Key switch: press FN when pressing F1-F12
POI excel cell wrap
Incremental database backup - DB incr DB full
ASP.NET-酒店管理系統
如何将数字字符串转换为整数
The world's most popular font editor FontCreator tool
FL Studio 20 unlimited trial fruit arranger Download