当前位置:网站首页>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 :

边栏推荐
- Lc669. Prune binary search tree
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
- 人体姿态估计的热图变成坐标点的两种方案
- 倒置残差的理解
- 【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
- 切面条 C语言
- 下班前几分钟,我弄清了v-model与.sync的区别
- 【无标题】
- Appium自动化测试基础 — APPium安装(一)
- 104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
猜你喜欢

Appium automated testing foundation - Supplement: introduction to desired capabilities parameters

Appium自动化测试基础 — APPium安装(一)

Appium自动化测试基础 — 补充:Desired Capabilities参数介绍

447-哔哩哔哩面经1

今日睡眠质量记录71分

台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条

Chen Tianqi's machine learning compilation course (free)

Easyexcel complex data export

Cutefishos system~

多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
随机推荐
Pytorch nn.functional.unfold()的简单理解与用法
Easyexcel complex data export
Slope compensation
分享一个一年经历两次裁员的程序员的一些感触
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
447-哔哩哔哩面经1
SAP 智能机器人流程自动化(iRPA)解决方案分享
Two schemes of transforming the heat map of human posture estimation into coordinate points
Réimpression de l'article csdn
转载csdn文章操作
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 release | geek headlines
下班前几分钟,我弄清了v-model与.sync的区别
Little red book scheme jumps to the specified page
【无标题】
el-input文本域字数限制,超过显示变红并禁止输入
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
数字货币:影响深远的创新
104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code
Redis configuration and optimization
[daily training] 326 Power of 3