当前位置:网站首页>map容器
map容器
2022-07-01 21:50:00 【爱学代码的学生】
1. map容器的构造函数和赋值操作
由于map容器中存储的数据都是成对出现的,因此我们需要使用pair来帮助我们完成存储
1.1 构造函数

这里我们只考虑拷贝构造
int main()
{
//map容器中的数据都是成对出现的,插入的数据必须是对组
map<string, int>m;
m.insert(pair<string, int>("张三", 18));
m.insert(pair<string, int>("李四", 28));
m.insert(pair<string, int>("王五", 17));
m.insert(pair<string, int>("赵二", 32));
Print(m);
cout << "------------" << endl;
//拷贝构造
map<string, int>m1(m);
Print(m1);
return 0;
}1.2 赋值操作

使用 = 来帮助我们完成赋值
int main()
{
//map容器中的数据都是成对出现的,插入的数据必须是对组
map<string, int>m;
m.insert(pair<string, int>("张三", 18));
m.insert(pair<string, int>("李四", 28));
m.insert(pair<string, int>("王五", 17));
m.insert(pair<string, int>("赵二", 32));
Print(m);
//赋值拷贝
cout << "------------" << endl;
map<string, int>m2;
m2 = m;
Print(m2);
return 0;
}2. map容的大器小和交换
2.1 大小的计算

这里我们使用容器中的size函数,它将返回map容器中的元素个数
void Print(const map<int, int>& m)
{
for (auto i = m.begin(); i != m.end(); i++)
{
cout << i->first << " " << i->second << endl;
}
}
//大小计算
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容器为空" << endl;
}
else
{
cout << "map容器不为空" << endl;
cout << "容器的大小是:" << m1.size() << endl;
}
}
int main()
{
test01();
return 0;
}
2.2 容器的交换

swap可以交换两个容器的空间
void Print(const map<int, int>& m)
{
for (auto i = m.begin(); i != m.end(); i++)
{
cout << i->first << " " << i->second << endl;
}
}
//交换操作
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 << "交换后" << endl;
m1.swap(m2);
Print(m1);
cout << "--------------" << endl;
Print(m2);
}
int main()
{
test02();
return 0;
}
3. map容器的插入和删除
3.1 map容器的插入

这里我们主要考虑map容器插入数据的方式:
第一种
m.insert(pair<int, int>(1, 10));第二种
m.insert(make_pair(2, 20));第三种
m.insert(map<int, int>::value_type(3, 30));第四种
m[4] = 40;添加后的结果:

因为map容器会将插入的值根据键值来进行排序,因为我们只考虑插入的方式即可,不考虑插入位置等
3.2 map容器的删除

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;
//第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
//第三种
m.insert(map<int, int>::value_type(3, 30));
//第四种
m[4] = 40;
Print(m);
cout << endl;
//1.按照迭代器删除
m.erase(m.begin());
Print(m);
cout << endl;
//2.删除键值为3
m.erase(3);
Print(m);
cout << endl;
//3.删除区间[m.begin(),m.end())的所有值
m.erase(m.begin(), m.end());
Print(m);
}
int main()
{
test01();
return 0;
}删除后的结果:

4. map容器的查找和统计
4.1 map容器的查找

如果找到查找的元素,则返回该元素的迭代器,否则返回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 << "没有找到" << endl;
}
else
{
cout << "first = " << p->first << " second = " << p->second;
}
p = m.find(3);
if (p == m.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "first = " << p->first << " second = " << p->second;
}
}
int main()
{
test01();
return 0;
}运行结果如下:

4.2 map容器的统计

查找键值k是否存在,如果存在则返回1,不存在则返回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;
}运行结果如下:

边栏推荐
- Lc669. Prune binary search tree
- 13th Blue Bridge Cup group B national tournament
- Clean up system cache and free memory under Linux
- 阿洛迷茫后的思考
- #yyds干货盘点# 解决名企真题:扭蛋机
- Why must digital transformation strategies include continuous testing?
- 删除AWS绑定的信用卡账户
- Yyds dry goods inventory # solve the real problem of famous enterprises: egg twisting machine
- C#/VB.NET 给PDF文档添加文本/图像水印
- The leader of the cloud native theme group of beacon Committee has a long way to go!
猜你喜欢
随机推荐
MySQL view exercise
447-哔哩哔哩面经1
Two schemes of transforming the heat map of human posture estimation into coordinate points
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
功能测试报告的编写
Mysql5.7 set password policy (etc. three-level password transformation)
MySQL5.7 设置密码策略(等保三级密码改造)
Wechat open platform scanning code login [easy to understand]
cvpr2022 human pose estiamtion
Awoo's favorite problem (priority queue)
Resttemplate remote call tool class
倒置残差的理解
【图像分割】2021-SegFormer NeurIPS
[literacy] deep / shallow, local / global features in machine learning image processing
20220701
Delete AWS bound credit card account
Mysql——》MyISAM存储引擎的索引
Understanding of inverted residuals
linux下清理系统缓存并释放内存
人体姿态估计的热图变成坐标点的两种方案



![[untitled]](/img/60/9a56e8b00c386779be13308515b24f.png)





