当前位置:网站首页>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#/VB.NET 给PDF文档添加文本/图像水印
- cvpr2022 human pose estiamtion
- LC501. Mode in binary search tree
- 完全注解的ssm框架搭建
- 友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
- Mysql database detailed learning tutorial
- 记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
- Indicator trap: seven KPI mistakes that it leaders are prone to make
- 【juc学习之路第8天】Condition
- 内部字段分隔符
猜你喜欢
随机推荐
【日常训练】326. 3 的幂
cvpr2022 human pose estiamtion
3DE resources have nothing or nothing wrong
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
flink sql 命令行 连接 yarn
Mysql——》索引存储模型推演
Chen Tianqi's machine learning compilation course (free)
Basic knowledge of ngnix
【日常训练】66. 加一
2020-ViT ICLR
Appium自动化测试基础 — 补充:Desired Capabilities参数介绍
隐藏用户的创建和使用
【JetCache】JetCache的使用方法与步骤
Wechat open platform scanning code login [easy to understand]
MySQL中对于事务的理解
恶意软件反向关闭EDR的原理、测试和反制思考
Indicator trap: seven KPI mistakes that it leaders are prone to make
MySQL数据库详细学习教程
Resttemplate remote call tool class
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