当前位置:网站首页>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
边栏推荐
- Hands on deep learning (33) -- style transfer
- 今日睡眠质量记录78分
- MySQL develops small mall management system
- El Table Radio select and hide the select all box
- Exercise 8-10 output student grades (20 points)
- Qtreeview+ custom model implementation example
- Kubernetes CNI 插件之Fabric
- C # use ffmpeg for audio transcoding
- Vs201 solution to failure to open source file HPP (or link library file)
- Golang 类型比较
猜你喜欢

Hands on deep learning (42) -- bi-directional recurrent neural network (BI RNN)

libmysqlclient. so. 20: cannot open shared object file: No such file or directory

【Day1】 deep-learning-basics

Hands on deep learning (33) -- style transfer

技术管理进阶——如何设计并跟进不同层级同学的绩效

How can people not love the amazing design of XXL job

How web pages interact with applets

Pcl:: fromrosmsg alarm failed to find match for field 'intensity'

leetcode1-3

For programmers, if it hurts the most...
随机推荐
Dynamic memory management
5g/4g wireless networking scheme for brand chain stores
PHP code audit 3 - system reload vulnerability
什么是 DevSecOps?2022 年的定义、流程、框架和最佳实践
[200 opencv routines] 218 Multi line italic text watermark
转载:等比数列的求和公式,及其推导过程
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
What are the advantages of automation?
How do microservices aggregate API documents? This wave of show~
Kotlin 集合操作汇总
Qtreeview+ custom model implementation example
Press the button wizard to learn how to fight monsters - identify the map, run the map, enter the gang and identify NPC
Doris / Clickhouse / Hudi, a phased summary in June
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
Basic principle of servlet and application of common API methods
leetcode1-3
Exercise 8-7 string sorting (20 points)
Exercise 8-10 output student grades (20 points)