C++笔记-34-map/multimap容器

map/multimap容器基本概念

  • map中所有元素都是pair
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素的键值自动排序

本质:map/multimap属于关联式容器,底层结构是用二叉树实现。

优点:可以根据key值快速找到value值

map和multimap区别:

  • map不允许容器中有重复key值元素
  • multimap允许容器中有重复key值元素

map容器构造和赋值

  • map<T1,T2>mp;//map默认构造函数
  • map(const map &mp);//拷贝构造函数
  • map& operator=(const map &mp);//重载等号操作符
cpp 复制代码
#include<iostream>
using namespace std;
#include<map> 
void print_map(const map<int, int>& m)
{
	for (auto it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}
	cout << endl;
}
void test()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(5, 50));
	print_map(m);
	map<int, int>m2(m);
	print_map(m2);
	map<int, int>m3;
	m3 = m2;
	print_map(m3);
}
int main()
{
	test();
}

map容器大小和交换

  • size;//返回容器中元素的数目
  • empty();//判断容器是否为空
  • swap(st);//交换两个集合容器
cpp 复制代码
#include<iostream>
using namespace std;
#include<map> 
void print_map(const map<int, int>& m)
{
	for (auto it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}
	cout << endl;
}
void test()
{
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(5, 50));
	if (!m.empty())
	{
		cout << "容器不为空" << endl;
		cout << "容器大小为:" << m.size() << endl;
	}
	else
	{
		cout << "容器为空" << endl;
	}
	map<int, int>m2;
	m2.insert({ 6,60 });
	m2.insert({ 7,70 });
	m2.insert({ 8,80 });
	m2.insert({ 9,90 });
	cout << "交换前:" << endl;
	print_map(m);
	print_map(m2);
	m.swap(m2);
	cout << "交换后" << endl;
	print_map(m);
	print_map(m2);
}
int main()
{
	test();
}

c++11之后引入了新标准,在插入元素的时候可以m.insert({int a,int b});这种形式来插入,就是不用写一长串的pair了,把两个元素用花括号包起来即可。

map容器插入和删除

  • insert(elem);//在容器中插入元素。
  • clear();//清除所有元素
  • erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
  • erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的迭代器。
  • erase(key);//删除容器中值为key的元素。
cpp 复制代码
#include<iostream>
using namespace std;
#include<map>
void print_map(const map<int, int>m)
{
	for (auto it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}
	cout << endl;
}
void test()
{
	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;
	/*
	不建议用这种方式去创建对象,例如下面打印m[5],此时key为5的键值对不存在
	那么就会自动创建一个key为5,值为0的键值对,可能会导致预期之外的结果。
	这种方式更适合用来访问key对应的value。
	*/
	cout << m[5] << endl;
	print_map(m);
	m.erase(m.begin());
	print_map(m);
	m.erase(3);
	print_map(m);
	m.erase(m.begin(), m.end());
	m.clear();
	print_map(m);
}
int main()
{
	test();
}

map容器查找和统计

find(key);//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();

count(key);//统计key的元素个数

cpp 复制代码
#include<iostream>
using namespace std;
#include<map>
void print_map(const map<int, int>m)
{
	for (auto it = m.begin(); it != m.end(); it++)
	{
		cout << it->first << " " << it->second << endl;
	}
	cout << endl;
}
void test()
{
	map<int, int>m;
	m.insert({ 1,10 });
	m.insert({ 2,20 });
	m.insert({ 3,30 });
	m.insert({ 4,40 });
	map<int, int>::iterator pos = m.find(3);
	if (pos != m.end())
	{
		cout << "查到了元素,key为:" << (*pos).first << " vlaue为:" << (*pos).second << endl;
	}
	else {
		cout << "未找到元素" << endl;
	}
	int num = m.count(3);
	cout << "num=" << num;
}
int main()
{
	test();
}

count和set里面的count用法几乎是一样的,值只有可能为0或者1,因为key不允许重复,但是在multimap里面获取的值可能是大于1的,multimap允许有重复的键。

map容器排序

利用仿函数,可以改变排序规则,和set是一样的

cpp 复制代码
#include<iostream>
using namespace std;
#include<map>
class compare {
public:
	bool operator()(int a, int b)const
	{
		return a > b;
	}
};
void test()
{
	map<int, int, compare>m;
	m.insert({ 1,10 });
	m.insert({ 2,20 });
	m.insert({ 3,30 });
	m.insert({ 4,40 });
	m.insert({ 5,50 });
	for (map<int, int, compare>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key=" << it->first << " value=" << it->second << endl;
	}
}
int main()
{
	test();
}

对于自定义数据类型,map必须要指定排序规则,否则插入的时候容器会不知道怎么排序,就会报错了,和set容器是一样的。

相关推荐
驭渊的小故事9 小时前
简单模板笔记
数据结构·笔记·算法
BD_Marathon9 小时前
设计模式——依赖倒转原则
java·开发语言·设计模式
devmoon9 小时前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
Evand J9 小时前
TDOA(到达时间差)的GDOP和CRLB计算的MATLAB例程,论文复现,附参考文献。GDOP:几何精度因子&CRLB:克拉美罗下界
开发语言·matlab·tdoa·crlb·gdop
野犬寒鸦9 小时前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
云姜.9 小时前
java抽象类和接口
java·开发语言
xyq20249 小时前
Pandas 安装指南
开发语言
智者知已应修善业10 小时前
【洛谷P9975奶牛被病毒传染最少数量推导,导出多样例】2025-2-26
c语言·c++·经验分享·笔记·算法·推荐算法
xixixin_10 小时前
【JavaScript 】从 || 到??:JavaScript 空值处理的最佳实践升级
开发语言·javascript·ecmascript
Trouvaille ~10 小时前
【Linux】应用层协议设计实战(一):自定义协议与网络计算器
linux·运维·服务器·网络·c++·http·应用层协议