【C++】关联式容器(map与multimap)的使用

7 篇文章 0 订阅

一、map 简介

C++ 中,std::mapmultimap都属于关联式容器,且都包含在头文件#include <map>中,其底层结构都是用二叉树实现的。

  • map中所有元素都是pairpair中第一个元素为键值(key),起索引的作用;第二个元素为实值(value),即映射值
  • 该类容器中元素在插入时都会根据元素的键值(key)自动排序

二、map 构造函数

构造函数原型解释
1map<T1, T2> mp默认构造
2map(const map &mp)拷贝构造

三、map 赋值操作

函数原型:=解释
1set& operator=(const set &st)重载=操作符

四、map 数据插入与删除

函数原型:insert、erase、clear解释
1insert(elem)在容器中插入元素
2erase(key)删除容器中键值为key的元素
3erase(pos)删除pos迭代器所指的元素,返回下一个元素的迭代器
4erase(beg, end)删除区间[beg,end)的所有元素,返回下一个元素的迭代器
5clear()清除所有元素

示例1:map<string, int>

#include <iostream>
#include <string>
#include <map>       //必须包含该头文件
using namespace std;

void printmap(map<string,int>& s)
{
	for (map<string,int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "键值:" << it->first << "\t实值:" << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<string,int> s1;
	//第1种插入方式
	s1["娃哈哈"] = 2;

	//第2种插入方式
	s1.insert(pair<string,int>("可乐", 3));

	//第3种插入方式
	s1.insert(make_pair("脉动", 4));

	//第4种插入方式
	s1.insert(map<string, int>::value_type("红牛", 5));
	
	cout << "插入后的元素:" << endl;
	printmap(s1);      //string类型key会根据首字母顺序

	s1.erase("娃哈哈");
	cout << "删除娃哈哈后的元素:" << endl;
	printmap(s1);

	s1.clear();
	cout << "清空后的元素:" << endl;
	printmap(s1);
}

int main()
{
	test01();
	system("pause");
	return 0;
}
//result
插入后的元素:
键值:红牛      实值:5
键值:可乐      实值:3
键值:脉动      实值:4
键值:娃哈哈    实值:2

删除娃哈哈后的元素:
键值:红牛      实值:5
键值:可乐      实值:3
键值:脉动      实值:4

清空后的元素:

注意:

  • map容器插入后的数据会根据key自动排序,string类型的key会根据首字母进行排序
  • map容器插入重复key值的元素会失败,元素数量不会发生改变

示例2:map<int, double>

#include <iostream>
#include <string>
#include <map>       //必须包含该头文件
using namespace std;

void printmap(map<int, double>& s)
{
	for (map<int, double>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "键值:" << it->first << "\t实值:" << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, double> s1;
	//第1种插入方式
	s1[1] = 1.001;

	//第2种插入方式
	s1.insert(pair<int, double>(4, 1.004));

	//第3种插入方式
	s1.insert(make_pair(2, 1.002));

	//第4种插入方式
	s1.insert(map<int, double>::value_type(3, 1.003));

	cout << "插入后的元素:" << endl;
	printmap(s1);      //string类型key会根据首字母顺序

	s1.erase(1);
	cout << "删除key=1的元素:" << endl;
	printmap(s1);

	s1.clear();
	cout << "清空后的元素:" << endl;
	printmap(s1);
}

int main()
{
	test01();
	system("pause");
	return 0;
}
//result
插入后的元素:
键值:1 实值:1.001
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004

删除key=1的元素:
键值:2 实值:1.002
键值:3 实值:1.003
键值:4 实值:1.004

清空后的元素:

五、map 查找与统计

函数原型:find、count解释
1find(key)查找key是否存在, 若存在则返回该键值元素的迭代器;若不存在则返回map.end()
2count(key)统计key的元素个数(对于map,结果为0或者1)

注意:

  • find(key)返回的是迭代器,可以通过iterator->firstiterator->second得到keyvalue
  • 因为map中元素不重复,所以count()返回0或者1

示例:

#include <iostream>
#include <string>
#include <map>       //必须包含该头文件
using namespace std;

void test01()
{
	map<string, int> s1;
	//插入insert
	s1["娃哈哈"] = 2;
	s1.insert(pair<string, int>("可乐", 3));
	s1.insert(make_pair("脉动", 4));
	s1.insert(map<string, int>::value_type("红牛", 5));

	//查找find
	map<string, int>::iterator pos = s1.find("脉动");
	if (pos != s1.end())
	{
		cout << "找到元素: key = " << pos->first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "未到元素key!";
	}

	//统计count
	int num1 = s1.count("可乐");
	cout << "可乐 num = " << num1 << endl;
	int num2 = s1.count("雪碧");
	cout << "雪碧 num = " << num2 << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
//result
找到元素: key = 脉动 value = 4
可乐 num = 1
雪碧 num = 0

六、map 元素数量与交换

函数原型:empty、size、swap解释
1empty()判断容器是否为空
2size()返回容器中元素的数量
3swap(set<T> & st)交换两个容器中的元素

注意:swap()交换的两个容器中包含元素的类型必须相同。

七、map 迭代器

函数原型:begin、end解释
1begin()返回一个迭代器,指向set容器的首元素
2end()返回一个迭代器,指向set容器的末尾元素的下一个位置
3rbegin()返回一个反向迭代器,指向set容器的最后一个元素
4rend()返回一个反向迭代器,指向set容器的首元素的上一个位置
5cbegin()返回一个const迭代器,指向set容器的首元素
6cend()返回一个const迭代器,指向set容器的末尾元素的下一个位置
7crbegin()返回一个const反向迭代器,指向set容器的末尾元素
8crend()返回一个const反向迭代器,指向set容器的首的上一个位置

注意:const iterator是一个指向const值的迭代器。迭代器本身可以修改,但是它不能被用来修改它所指向的值。

八、map 容器排序

  • map容器默认排序规则为key值从小到大
  • 利用仿函数,可以改变排序规则
  • 对于自定义数据类型,map必须指定key值排序规则,同setset使用方法

示例:

#include <iostream>
#include <string>
#include <map>       //必须包含该头文件
using namespace std;

//仿函数,重载operator()运算符
class Compare
{
public:
    //Visual Studio的编译器此处需添加const,具有指定const-volatile类型的变量只能调用具有相同或更大const-volatile限定定义的成员函数
	bool operator()(int v1, int v2)const  
	{
		return v1 > v2;
	}
};

void test01()
{
	map<int,string,Compare> s1;  //指定排序规则, 从大到小
	s1.insert(pair<int, string>(1, "李四"));
	s1.insert(pair<int, string>(3, "张三"));
	s1.insert(pair<int, string>(4, "王五"));
	s1.insert(pair<int, string>(2, "赵六"));
	
	for (map<int, string>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "键值:" << it->first << "\t实值:" << it->second << endl;
	}
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
//result
键值:4 实值:王五
键值:3 实值:张三
键值:2 实值:赵六
键值:1 实值:李四

九、map 与 multimap 区别

  • map不允许容器中有重复的元素
  • multimap允许容器中有重复的元素
  • map插入数据的同时会返回插入结果,表示插入是否成功
  • multimap不会检测数据,因此可以插入重复数据

示例:multimapfind方法

#include <iostream>
#include <string>
#include <map>       //必须包含该头文件
using namespace std;

void test01()
{
	multimap<int, string> s1;
	s1.insert(pair<int, string>(1, "李四"));
	s1.insert(pair<int, string>(3, "张三"));
	s1.insert(pair<int, string>(1, "丁一"));
	s1.insert(pair<int, string>(4, "王五"));
	s1.insert(pair<int, string>(2, "赵六"));
	s1.insert(pair<int, string>(1, "吴二"));

	int sum = s1.count(1);
	cout << "key = 1的元素数量:" << sum << endl;

	//因为map会自动排列,key相同的元素必定连续排列,所以从第一个find(1)位置遍历count(1)次
	multimap<int, string>::iterator it = s1.find(1);
	for (int i = 0; i != s1.count(1); i++,it++)
	{
		cout << "键值:" << it->first << "\t实值:" << it->second << endl;
	}
	cout << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}
//result
key = 1的元素数量:3
键值:1 实值:李四
键值:1 实值:丁一
键值:1 实值:吴二

注意: 因为multimap会自动排列,key相同的元素必定连续排列,所以从第一个find(1)位置遍历count(1)次,即可获得所有key = 1的键值对元素。


如果这篇文章对你有所帮助,渴望获得你的一个点赞!

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OpenC++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值