当前位置:网站首页>Map container
Map container
2022-07-01 22:53:00 【Code loving students】
1. map Constructor and assignment operation of container
because map The data stored in the container appears in pairs , So we need to use pair To help us complete storage
1.1 Constructors

Here we only consider copy construction
int main()
{
//map The data in the container appears in pairs , The inserted data must be a pair of groups
map<string, int>m;
m.insert(pair<string, int>(" Zhang San ", 18));
m.insert(pair<string, int>(" Li Si ", 28));
m.insert(pair<string, int>(" Wang Wu ", 17));
m.insert(pair<string, int>(" Zhao er ", 32));
Print(m);
cout << "------------" << endl;
// Copy structure
map<string, int>m1(m);
Print(m1);
return 0;
}1.2 Assignment operation

Use = To help us complete the assignment
int main()
{
//map The data in the container appears in pairs , The inserted data must be a pair of groups
map<string, int>m;
m.insert(pair<string, int>(" Zhang San ", 18));
m.insert(pair<string, int>(" Li Si ", 28));
m.insert(pair<string, int>(" Wang Wu ", 17));
m.insert(pair<string, int>(" Zhao er ", 32));
Print(m);
// Copy of assignment
cout << "------------" << endl;
map<string, int>m2;
m2 = m;
Print(m2);
return 0;
}2. map Large capacity, small capacity and Exchange
2.1 Calculation of size

Here we use size function , It will return map The number of elements in the container
void Print(const map<int, int>& m)
{
for (auto i = m.begin(); i != m.end(); i++)
{
cout << i->first << " " << i->second << endl;
}
}
// Size calculation
void test01()
{
map<int, int>m1;
for (int i = 0; i < 5; i++)
{
m1.insert(pair<int, int>(i, i));
}
Print(m1);
if (m1.empty())
{
cout << "map The container is empty " << endl;
}
else
{
cout << "map The container is not empty " << endl;
cout << " The size of the container is :" << m1.size() << endl;
}
}
int main()
{
test01();
return 0;
}
2.2 Exchange of containers

swap Space between two containers can be exchanged
void Print(const map<int, int>& m)
{
for (auto i = m.begin(); i != m.end(); i++)
{
cout << i->first << " " << i->second << endl;
}
}
// Switching operation
void test02()
{
map<int, int>m1;
for (int i = 0; i < 5; i++)
{
m1.insert(pair<int, int>(i, i));
}
Print(m1);
map<int, int>m2;
for (int i = 0; i < 3; i++)
{
m2.insert(pair<int, int>(i + 3, i + 3));
}
cout << "--------------" << endl;
Print(m2);
cout << " After exchanging " << endl;
m1.swap(m2);
Print(m1);
cout << "--------------" << endl;
Print(m2);
}
int main()
{
test02();
return 0;
}
3. map The insertion and deletion of containers
3.1 map Insertion of container

Here we mainly consider map How the container inserts data :
The first one is
m.insert(pair<int, int>(1, 10));The second kind
m.insert(make_pair(2, 20));The third kind of
m.insert(map<int, int>::value_type(3, 30));A fourth
m[4] = 40;Added results :

because map The container will insert the value according to Key value To sort , Because we only consider the way of insertion , Regardless of the insertion position
3.2 map Deletion of container

void Print(const map<int, int>& m)
{
for (auto i = m.begin(); i != m.end(); i++)
{
cout <<"key = "<< i->first << " value = " << i->second << endl;
}
}
void test01()
{
map<int, int>m;
// The first one is
m.insert(pair<int, int>(1, 10));
// The second kind
m.insert(make_pair(2, 20));
// The third kind of
m.insert(map<int, int>::value_type(3, 30));
// A fourth
m[4] = 40;
Print(m);
cout << endl;
//1. Delete by iterator
m.erase(m.begin());
Print(m);
cout << endl;
//2. The delete key value is 3
m.erase(3);
Print(m);
cout << endl;
//3. Delete range [m.begin(),m.end()) All values
m.erase(m.begin(), m.end());
Print(m);
}
int main()
{
test01();
return 0;
}Results after deletion :

4. map Container search and Statistics
4.1 map Container lookup

If you find the element you are looking for , Returns the iterator of the element , Otherwise return to map::end
void test01()
{
map<int, int>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
auto p = m.find(55);
if (p == m.end())
{
cout << " Can't find " << endl;
}
else
{
cout << "first = " << p->first << " second = " << p->second;
}
p = m.find(3);
if (p == m.end())
{
cout << " Can't find " << endl;
}
else
{
cout << "first = " << p->first << " second = " << p->second;
}
}
int main()
{
test01();
return 0;
}The operation results are as follows :

4.2 map Statistics of containers

Find key values k Whether there is , Return if present 1, Returns if it does not exist 0
void test01()
{
map<int, int>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
cout << m.count(3) << endl;
cout << m.count(5) << endl;
}
int main()
{
test01();
return 0;
}The operation results are as follows :

边栏推荐
- 聊一聊Zabbix都监控哪些参数
- The fixed assets management subsystem reports are divided into what categories and which accounts are included
- Redis configuration and optimization
- FFMpeg学习笔记
- Flynk SQL client uses comparison and is familiar with official documents
- The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
- 3DE resources have nothing or nothing wrong
- Lc669. Prune binary search tree
- General use of qstringlist
- 【c语言】malloc函数详解[通俗易懂]
猜你喜欢

SAP GUI 里的收藏夹事务码管理工具

MySQL MHA high availability configuration and failover

转载csdn文章操作

Yolov5.5 call local camera

Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo

# CutefishOS系统~

Delete AWS bound credit card account

Configure filter

H5 model trained by keras to tflite

数字货币:影响深远的创新
随机推荐
SAP 智能机器人流程自动化(iRPA)解决方案分享
Today's sleep quality record 71 points
[image segmentation] 2021 segformer neurips
激发新动能 多地发力数字经济
FFMpeg学习笔记
【JetCache】JetCache的使用方法与步骤
mixconv代码
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
H5 model trained by keras to tflite
Appium automation test foundation - appium installation (I)
Slope compensation
Configure filter
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
SAP UI5 应用开发教程之一百零四 - SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
Selection of all-optical technology in the park - Part 2
Appium automated testing foundation - Supplement: introduction to desired capabilities parameters
How to write a performance test plan
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
Understanding of indexes in MySQL
[jetcache] how to use jetcache