当前位置:网站首页>Map container
Map container
2022-07-04 10:15:00 【Wildcraner】
One 、 Insert the way
map<int, int> m;
// The first way to insert
m.insert(pair<int, int>(1, 10));
// The second insertion method
m.insert(make_pair(2, 20));
// Third insertion method
m.insert(map<int, int>::value_type(3, 30));
// Fourth insertion method
m[4] = 40;
result
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40
Be careful
If key = 1 value = 10 Already exist , How to change ?
map<int, int> m;
// The first way to insert
m.insert(pair<int, int>(1, 10));
m.insert(pair<int, int>(1, 12));
// The second insertion method
m.insert(make_pair(1, 20));
// Third insertion method
m.insert(map<int, int>::value_type(1, 30));
// Fourth insertion method
//m[1] = 40;
printMap(m);
Running results key = 1 value = 10
So we need to change it in the fourth way :m[1] = 40;
Two 、 Traverse ( Using iterators )
void printMap(map<int, int>& m)
{
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
map<int, int>::iterator it -> Define an iterator itit->first map Container of key value it->second map Container of value value
边栏推荐
- 华为联机对战如何提升玩家匹配成功几率
- System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
- Exercise 7-8 converting strings to decimal integers (15 points)
- Hands on deep learning (32) -- fully connected convolutional neural network FCN
- Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
- 对于程序员来说,伤害力度最大的话。。。
- Application of safety monitoring in zhizhilu Denggan reservoir area
- 5g/4g wireless networking scheme for brand chain stores
- Hands on deep learning (33) -- style transfer
- C language pointer classic interview question - the first bullet
猜你喜欢

C语言指针面试题——第二弹

El Table Radio select and hide the select all box
Web端自动化测试失败原因汇总

xxl-job惊艳的设计,怎能叫人不爱

Nuxt reports an error: render function or template not defined in component: anonymous

Hands on deep learning (40) -- short and long term memory network (LSTM)

QTreeView+自定义Model实现示例

leetcode1-3

Matlab tips (25) competitive neural network and SOM neural network

C language pointer classic interview question - the first bullet
随机推荐
Custom type: structure, enumeration, union
IIS configure FTP website
Kubernetes CNI 插件之Fabric
AUTOSAR from getting started to mastering 100 lectures (106) - SOA in domain controllers
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Advanced technology management - how to design and follow up the performance of students at different levels
Development guidance document of CMDB
El Table Radio select and hide the select all box
uniapp 处理过去时间对比现在时间的时间差 如刚刚、几分钟前,几小时前,几个月前
Histogram equalization
MongoDB数据日期显示相差8小时 原因和解决方案
回复评论的sql
Dos:disk operating system, including core startup program and command program
On Multus CNI
Kotlin set operation summary
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
Golang Modules
QTreeView+自定义Model实现示例
Doris / Clickhouse / Hudi, a phased summary in June