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

边栏推荐
猜你喜欢

Ffmpeg learning notes

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语言中的setjmp和longjmp,来实现异常捕获和协程

Turn -- bring it and use it: share a gadget for checking memory leaks

Appium automation test foundation - appium installation (I)

Flynk SQL client uses comparison and is familiar with official documents

Multi picture alert ~ comparison of Huawei ECs and Alibaba cloud ECS

Kubernetes create service access pod

FFMpeg学习笔记

Slope compensation
随机推荐
MySQL中对于索引的理解
Appium自动化测试基础 — APPium安装(一)
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
利用SecureCRTPortable远程连接虚拟机
Origin2018 installation tutorial "recommended collection"
Kubernetes创建Service访问Pod
性能测试计划怎么编写
MySQL MHA high availability configuration and failover
[jetcache] how to use jetcache
Chen Tianqi's machine learning compilation course (free)
twenty million two hundred and twenty thousand seven hundred and one
恶意软件反向关闭EDR的原理、测试和反制思考
Mysql5.7 set password policy (etc. three-level password transformation)
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
Metauniverse may become a new direction of Internet development
Réimpression de l'article csdn
Flink SQL command line connection yarn
3DE resources have nothing or nothing wrong
Sogou wechat app reverse (II) so layer
Today's sleep quality record 71 points