当前位置:网站首页>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;
}
边栏推荐
- Key switch: press FN when pressing F1-F12
- 1. Hal driven development
- Redis things
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- [OBS] configFile in ini format of OBS
- Incremental database backup - DB incr DB full
- CSRF
- After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
- 按键切换:按F1-F12都需要按Fn
- R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
猜你喜欢

基于turtlebot3实现SLAM建图及自主导航仿真

Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power

Incremental database backup - DB incr DB full

Excel快速跨表复制粘贴

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

Viewing binary bin files with notepad++ editor

用了这么久线程池,你真的知道如何合理配置线程数吗?

FL Studio 20无限试用版水果编曲下载

金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~

Excel表格转到Word中,表格不超边缘纸张范围
随机推荐
C语言二维数组
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
Illustrated network: what is virtual router redundancy protocol VRRP?
Processes and threads
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
Some common terms
Phpcms prompt message page Jump to showmessage
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
进程与线程
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
抓包整理外篇fiddler———— 会话栏与过滤器[二]
The world's most popular font editor FontCreator tool
如何成为一名高级数字 IC 设计工程师(1-5)Verilog 编码语法篇:操作数
鸿蒙第三次培训(项目实训)
ASP.NET-酒店管理系統
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
Viewing binary bin files with notepad++ editor
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
asyncio 警告 DeprecationWarning: There is no current event loop