当前位置:网站首页>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;
}运行结果如下:

边栏推荐
- 切面条 C语言
- C#/VB.NET 给PDF文档添加文本/图像水印
- #yyds干货盘点# 解决名企真题:扭蛋机
- Delete AWS bound credit card account
- 固定资产管理子系统报表分为什么大类,包括哪些科目
- 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
- Explain kubernetes network model in detail
- 效率提升 - 鼓捣个性化容器开发环境
- 基准环路增益与相位裕度的测量
- There is no signal in HDMI in computer games caused by memory, so it crashes
猜你喜欢
随机推荐
记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
Selection of all-optical technology in the park - Part 2
详解JMM
Pytorch's code for visualizing feature maps after training its own network
Mixconv code
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
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
园区全光技术选型-中篇
YOLOv5.5 调用本地摄像头
效率提升 - 鼓捣个性化容器开发环境
mixconv代码
2020-ViT ICLR
MySQL MHA high availability configuration and failover
隐藏用户的创建和使用
[untitled]
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
Mysql——》索引存储模型推演
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
nn. Parameter] pytoch feature fusion adaptive weight setting (learnable weight use)
Rust语言——小小白的入门学习05





![快乐数[环类问题之快慢指针]](/img/37/5c94b9b062a54067a50918f94e61ea.png)


