C++经典问题_25 STL(九) map/multimap 容器

一. map容器的基本概念

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

本质:

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

优点:

可以根据key值快速查找到value的值

map和multimap的区别?

  1. map不允许容器中有重复的key值元素
  2. multimap中允许容器中有重复的key值元素

二. map的构造和赋值

函数原型:
构造

map<T1,T2> mp; // map的默认构造函数
map(const map& mp); // 拷贝构造函数

赋值:

map& operator=(const map& mp); // 重载等号操作符

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<map>

void print_map(const map<int, int> &mp)
{
	for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); it++)
	{
		cout << "key = " << it->first << " value = " << it->second << endl;
	}
	cout << "===================================================" << endl;
}


void test_01(void)
{
	// 创建map容器
	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));
	print_map(m);

	// 拷贝构造
	map<int, int> m2(m);
	print_map(m2);

	// 赋值操作符
	map<int, int> m3;
	m3 = m2;
	print_map(m3);
}
int main()
{
	test_01();

	system("pause");
	return 0;
}

三. map的大小和交换

函数原型:

  1. size(): 返回容器中元素的个数
  2. empty(): 判断容器是否为空
  3. swap(st); 交换两个集合容器
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<map>

void print_map(const map<int, int> &m)
{
	for (map<int, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "key = " << (*it).first << " value = " << (*it).second << endl;
	}
	cout << "------------------------------" << endl;
}

void test_01(void)
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(1, 100)); // 这里插入不成功
	// map插入的时候,会根据key进行排序,默认升序
	// map插入的时候,如果key已经存在,则插入失败
	print_map(m);

	// 查看map的大小和是否为空
	if (m.empty())
	{
		cout << "map为空!" << endl;
	}
	else
	{
		cout << "map不为空,map的大小为: " << m.size() << endl;
	}

	//map<int, double> m2; // 如果类型不一样,是否可以交换呢
	//m2.insert(pair<int, double>(3, 3.14));

	//m.swap(m2); 这里会报错,看来如果类型不一样,不支持交换操作


	map<int, int> m2;
	m2.insert(pair<int, int>(10, 100));
	m2.insert(pair<int, int>(20, 200));

	cout << "交换前: " << endl;
	cout << "m = "<<  endl;
	print_map(m);
	cout << "m2 = " <<  endl;
	print_map(m2);

	m.swap(m2);
	cout << "交换后: " << endl;
	cout << "m = " << endl;
	print_map(m);
	cout << "m2 = " << endl;
	print_map(m2);


}



int main()
{
	test_01();
	system("pause");
	return 0;
}

四. map的插入和删除

函数原型:

isnert(elem); // 在容器中插入元素
clear(); 清除所有的元素
erase(pos); 删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(begin,end); 删除区间[begin,end]的所有元素,返回下一个元素的迭代器
erase(key); 删除容器中值为key的元素

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<string>
#include<map>

void print_map(const map<string, int> &m)
{
	for (map<string, int>::const_iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "姓名: " << it->first << " 分数: " << it->second << endl;
	}
	cout << "----------------------------------" << endl;
}

void test_01(void)
{
	// 创建map对象
	map<string, int> m;
	m.insert(pair<string, int>("张三", 100));
	m.insert(pair<string, int>("李四", 90));
	m.insert(pair<string, int>("王五", 82));
	m.insert(pair<string, int>("小红", 75));
	m.insert(pair<string, int>("小强", 60));
	print_map(m);
	// 这里打印结果
	/*
	*   姓名: 李四 分数: 90
		姓名: 王五 分数: 82
		姓名: 小红 分数: 75
		姓名: 小强 分数: 60
		姓名: 张三 分数: 100
	*/
	// 删除pos迭代器所指定的元素,只能用递增和递减,不能使用 +- 操作符
	// 原因就是map对象也不支持随机访问
	//m.erase(m.begin() + 1); 
	// erase(pos)有返回值,返回的是下一个元素的迭代器
	map<string, int>::iterator it;
	it = m.erase(++m.begin()); // 这里删除的是第二个元素,也就是王五,然后返回下一个元素
	// 当前it指向的是小红
	cout << "当前迭代器指向的内容: " << "姓名: " << it->first << " 年龄: " << it->second << endl;

	// 删除小红
	m.erase("小红");
	print_map(m);

	// 然后删除区间元素,返回的迭代器是下一个元素
	// 因为删除的是end()的上一个位置,然后--,表示的是m.end()的上一个位置.
	// 所以这里其实删除的是m.end()的上两个位置,最后it指向的是m.end()的上一个位置
	// 也就是最后一个元素
	// earse(begin,end) 删除的是[begin,end]区间内的元素,end()这个位置是没有元素的,所以是不包括end()的
	it = m.erase(m.begin(), --m.end());

	cout << "当前迭代器指向的内容: " << "姓名: " << it->first << " 年龄: " << it->second << endl;
}


int main()
{

	test_01();
	system("pause");
	return 0;
}

五. map的查找和统计

函数原型:

  1. find(key): 查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
    可以通过返回值是否等于map.end()来判断是否查找到了元素
  2. count(key): 统计具有相同key的元素的个数,如果是普通的map,这个值要不就是1,要不就是0
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
using namespace std;
#include<map>

void test_01(void)
{
	map<int, int> m;
	m.insert(make_pair<int, int>(1, 10));
	m.insert(make_pair<int, int>(2, 20));
	m.insert(make_pair<int, int>(3, 30));
	m.insert(make_pair<int, int>(4, 40));
	m.insert(make_pair<int, int>(1, 50));

	// 查找
	map<int, int>::iterator pos;
	pos = m.find(1);
	if (pos != m.end())
	{
		cout << "查找成功!" << " key = " << pos->first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "查找失败" << endl;
	}

	pos = m.find(100);
	if (pos != m.end())
	{
		cout << "查找成功!" << " key = " << pos->first << " value = " << pos->second << endl;
	}
	else
	{
		cout << "查找失败" << endl;
	}

	cout << "key = 1 的个数: " << m.count(1) << endl;
	cout << "key = 100 的个数: " << m.count(100) << endl;
}

int main()
{
	test_01();

	system("pause");
	return 0;
}

六.map容器排序

  1. map容器默认的排序规则是按照key值进行从小到大的排序
  2. 利用仿函数,可以改变排序规则
  3. 需要在创建map的时候,在模板参数里面添加仿函数参数
  4. 注意的是仿函数要写成常函数,也就是具有const属性的函数

七 案例

案例描述:
在这里插入图片描述

/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/

#include <iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;

class Person
{
public:
	Person(string name, int salary, string dept)
	{
		this->mName = name;
		this->mSalary = salary;
		this->mDept = dept;
	}
public:
	string mName;
	int mSalary;
	string mDept;
};

// 创建10名员工
void create_person(vector<Person> &vPerson)
{
	string nameList = "ABCDEFGHIJ";
	vector<string> vDept;
	vDept.push_back("策划");
	vDept.push_back("美术");
	vDept.push_back("研发");

	for (int i = 0; i < nameList.size(); i++)
	{
		string name = "员工";
		name += nameList[i];
		int salary = rand() % 10000 + 1 +  5000; // 工资 5000 ~ 15000 之间
		string dept = vDept[rand() % 3]; // 随机部门
		Person p = Person(name, salary, dept);
		vPerson.push_back(p); // 放到vector容器中.
	}

}

void group_person(vector<Person> &vPerson, multimap<string, Person> &mulMap)
{
	// 遍历vector,然后将数据存入到mulMap中去
	for (int i = 0; i < vPerson.size(); i++)
	{
		mulMap.insert(pair<string, Person>(vPerson[i].mDept, vPerson[i]));
	}
}

void show_person_info_by_dept(const multimap<string, Person> &mulMap)
{
	string deptShow;
	// 先取出来第一个部门
	multimap<string, Person>::const_iterator it = mulMap.begin();
	deptShow = it->first; // 第一个部门
	cout << "部门: " << deptShow << " 的人员信息如下: " << endl;
	// 分部门显示信息
	for (multimap<string, Person>::const_iterator it = mulMap.begin(); it != mulMap.end(); it++)
	{
		if (it->first == deptShow)
		{
			// 如果是相同的部门,就直接显示
			cout << "姓名: " << it->second.mName << ", 工资: " << it->second.mSalary << endl;
		}
		else
		{
			cout << "================================" << endl;
			// 如果是不同的部门
			deptShow = it->first; // 更换显示的部门
			cout << "部门: " << deptShow << " 的人员信息如下: " << endl;
			cout << "姓名: " << it->second.mName << ", 工资: " << it->second.mSalary << endl;
		}
	}
}


int main()
{
	vector<Person> vPerson;
	create_person(vPerson); // 先创建员工
	multimap<string, Person> mulMap;
	group_person(vPerson,mulMap); // 对员工进行按部门分组
	show_person_info_by_dept(mulMap);
	system("pause");
	return 0;
}

结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值