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容器是一样的。

相关推荐
لا معنى له2 小时前
目标检测的内涵、发展和经典模型--学习笔记
人工智能·笔记·深度学习·学习·目标检测·机器学习
flying robot5 小时前
centos7系统配置
笔记
橘子真甜~5 小时前
C/C++ Linux网络编程15 - 网络层IP协议
linux·网络·c++·网络协议·tcp/ip·计算机网络·网络层
小浣熊熊熊熊熊熊熊丶5 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子5 小时前
JDK 安装配置
java·开发语言
星哥说事5 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....6 小时前
Miniconda使用
开发语言·python
zfj3216 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧6 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop6 小时前
Aes加密 GCM java
java·开发语言·python