当前位置:网站首页>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;
}
边栏推荐
- C语言日志库zlog基本使用
- PHP server interacts with redis with a large number of close_ Wait analysis
- 一些常用术语
- 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
- 如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
- How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)
- repo ~ 常用命令
- phpcms 提示信息頁面跳轉showmessage
- Function details of CorelDRAW graphics suite 2022
猜你喜欢

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

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

鸿蒙第四次培训

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

The world's most popular font editor FontCreator tool

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

(2) Base

Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
![[OBS] configFile in ini format of OBS](/img/b2/0b130cee6ea884557a30e4b408f49e.png)
[OBS] configFile in ini format of OBS

How should intermediate software designers prepare for the soft test
随机推荐
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
asyncio 警告 DeprecationWarning: There is no current event loop
ASP. Net hotel management system
Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3
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
The manuscript will be revised for release tonight. But, still stuck here, maybe what you need is a paragraph.
鸿蒙第三次培训(项目实训)
GCC compilation process and dynamic link library and static link library
How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
Driver development based on I2C protocol
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
机器学习 3.2 决策树模型 学习笔记(待补)
Phpcms prompt message page Jump to showmessage
Résumé des questions d'entrevue (2) Modèle io, ensemble, principe NiO, pénétration du cache, avalanche de rupture
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
Dynamic programming (interval DP)
Nestjs配置服务,配置Cookie和Session
银泰百货点燃城市“夜经济”
AMS series - application startup process