当前位置:网站首页>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;
}
边栏推荐
- 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
- 金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
- 活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
- Oracle 11g single machine cold standby database
- Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
- . \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
- 00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
- R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
- ASP.NET-酒店管理系统
- FL Studio 20 unlimited trial fruit arranger Download
猜你喜欢

Matlab extracts numerical data from irregular txt files (simple and practical)

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

Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution

How should intermediate software designers prepare for the soft test

Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises

Intel 13th generation core flagship exposure, single core 5.5ghz

软考中级软件设计师该怎么备考

Redis things

Modular programming of single chip microcomputer

Kubernetes 三打探针及探针方式
随机推荐
机器学习 3.2 决策树模型 学习笔记(待补)
2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
Execute kubectl on Tencent cloud container service node
聊聊Flink框架中的状态管理机制
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
C language AES encryption and decryption
JGG专刊征稿:时空组学
1. Hal driven development
ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
How to make others fear you
软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
(2) Base
Leetcode 46: full arrangement
软考中级软件设计师该怎么备考
Driver development based on I2C protocol
[VTK] vtkPolydataToImageStencil 源码解读
The excel table is transferred to word, and the table does not exceed the edge paper range
鸿蒙第四次培训
How should intermediate software designers prepare for the soft test
一文搞懂Go语言Context