当前位置:网站首页>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 :
边栏推荐
- nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
- MySQL MHA high availability configuration and failover
- Reprint CSDN article operation
- 隐藏用户的创建和使用
- Quantifiers of regular series
- 好友新书发布,祝贺(送福利)
- Cut noodles C language
- 陈天奇的机器学习编译课(免费)
- 447 Bili Bili noodles warp 1
- Learn MySQL from scratch - database and data table operations
猜你喜欢
内部字段分隔符
Flynk SQL client uses comparison and is familiar with official documents
2020-ViT ICLR
Understanding of indexes in MySQL
104. SAP UI5 表格控件的支持复选(Multi-Select)以及如何用代码一次选中多个表格行项目
Kubernetes create service access pod
Share some feelings of a programmer who has experienced layoffs twice a year
“信任机器”为发展赋能
Learn MySQL from scratch - database and data table operations
Favorite transaction code management tool in SAP GUI
随机推荐
Delete AWS bound credit card account
【日常训练】66. 加一
Reprint CSDN article operation
Appium automation test foundation - appium installation (I)
今日睡眠质量记录71分
人体姿态估计的热图变成坐标点的两种方案
Slope compensation
QStringList 的常规使用
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
元宇宙可能成为互联网发展的新方向
FFMpeg学习笔记
MySQL MHA high availability configuration and failover
【图像分割】2021-SegFormer NeurIPS
Friendly serial assistant tutorial_ How to configure friendly serial port debugging assistant - tutorial on using friendly serial port debugging assistant
Understanding of inverted residuals
Quantifiers of regular series
【无标题】
[literacy] deep / shallow, local / global features in machine learning image processing
internal field separator
[untitled]