C++学习笔记(二十八)

这篇博客详细介绍了C++中的map容器,包括其基本概念、构造与赋值、大小和交换、插入与删除、查找与统计以及排序。此外,还通过实例展示了如何使用map进行员工分组,将员工按随机分配的部门进行组织。博客内容涵盖了map的主要操作和使用场景。
摘要由CSDN通过智能技术生成

在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十八篇博客。

本篇博客介绍了C++的map容器,并实现了员工分组。

本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

目录

map容器

map基本概念

map构造和赋值

map大小和交换

map插入和删除

map查找和统计

map排序

员工分组


map容器

map基本概念

map里面所有元素都是pair。pair的第一个元素为键,起到索引作用,第二个元素为值。所有元素会根据键值自动排序。

mapmultimap属于关联式容器,底层结构用二叉树实现。map不允许键值重复,而multimap允许键值重复。使用map或multimap都要包含头文件map

map构造和赋值

map<T1,T2> mp创建一个map容器,键的类型为T1,值得类型为T2。

map(const map &mp)是拷贝构造函数。

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

#include<iostream>
#include<map>
using namespace std;
int main(void)
{
	map<int, int> m1;
	m1.insert(pair<int, int>(25, 75));
	m1.insert(pair<int, int>(20, 20));
	m1.insert(pair<int, int>(10, 15));
	m1.insert(pair<int, int>(5, 20));
	m1.insert(pair<int, int>(15, 25));

	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); ++it) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;

	map<int, int>m2(m1);
	for (map<int, int>::iterator it = m2.begin(); it != m2.end(); ++it) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;

	map<int, int>m3;
	m3 = m2;
	for (map<int, int>::iterator it = m3.begin(); it != m3.end(); ++it) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;
	return 0;
}

程序的输出是:

5   20
10   15
15   25
20   20
25   75

5   20
10   15
15   25
20   20
25   75

5   20
10   15
15   25
20   20
25   75
 

map大小和交换

size()返回容器中元素个数。

empty()判断容器是否为空。

swap(mp)交换两个容器。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{
	map<int, int>m1;
	m1.insert(pair<int, int>(10, 100));
	m1.insert(pair<int, int>(20, 200));
	m1.insert(pair<int, int>(30, 300));
	m1.insert(pair<int, int>(40, 400));
	map<int, int>m2;
	m2.insert(pair<int, int>(60, 600));
	m2.insert(pair<int, int>(70, 700));
	m2.insert(pair<int, int>(80, 800));
	m2.insert(pair<int, int>(90, 900));

	cout << m1.empty() << endl;
	cout << m1.size() << endl;

	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;

	for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;
	m1.swap(m2);

	for (map<int, int>::iterator it = m1.begin(); it != m1.end(); it++) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;
	for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;
	return 0;
}

程序的输出是:

0
4
10   100
20   200
30   300
40   400

60   600
70   700
80   800
90   900

60   600
70   700
80   800
90   900

10   100
20   200
30   300
40   400
 

map插入和删除

insert(elem)将elem插入容器中。

clear()删除所有元素。

erase(pos)删除pos迭代器所指元素。

erase(beg,end)删除beg到end的所有元素,包括beg不包括end。

erase(key)删除键值为key的元素。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{
	map<int, int>m;
	m.insert(pair<int, int>(10, 100));
	m.insert(pair<int, int>(20, 200));
	m.insert(make_pair(30, 300));
	m.insert(make_pair(40, 400));
	for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;

	m.erase(30);
	for (map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
		cout << it->first << "   " << it->second << endl;
	}
	cout << endl;
	return 0;
}

程序的输出是:

10   100
20   200
30   300
40   400

10   100
20   200
40   400
 

map查找和统计

find(key)查找key为键的是否存在,存在则返回指向的迭代器,否则返回.end。

count(key)返回key为键的元素个数。

map不能插入键重复的元素,否则以第一次为准。

#include<iostream>
#include<map>
using namespace std;
int main(void)
{
	map<int, int>m;
	m.insert(pair<int, int>(10, 100));
	m.insert(pair<int, int>(70, 700));
	m.insert(make_pair(60, 600));
	m.insert(make_pair(30, 300));

	map<int, int>::iterator it;
	it = m.find(50);
	if (it == m.end()) {
		cout << "not found" << endl;
	}
	else {
		cout << "found" << endl;
	}

	it = m.find(60);
	if (it == m.end()) {
		cout << "not found" << endl;
	}
	else {
		cout << "found" << endl;
	}

	cout << m.count(30) << endl;
	cout << m.count(20) << endl;
	return 0;
}

程序的输出是:

not found
found
1
0

map排序

对于自定义类型,要用仿函数指定排序规则。也可以用仿函数改变默认排序规则。

#include<iostream>
#include<map>
using namespace std;
class compare
{
public:
	bool operator() (int a, int b) const
	{
		return a > b;
	}
};
int main(void)
{
	map<int, int, compare> m;
	m.insert(pair <int, int>(30, 50));
	m.insert(pair <int, int>(40, 60));
	m.insert(make_pair(10, 20));
	m.insert(make_pair(25, 75));

	for (map<int, int, compare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << it->first << "   " << it->second << endl;
	}
	return 0;
}

程序中用仿函数将排序规则改为降序。程序的输出是:

40   60
30   50
25   75
10   20


员工分组

公司招聘了十二个员工,进入公司后要指派在哪个部门工作。员工信息包括姓名和工资。部门有三个部分(程序中用ABC代替)。随机分配给员工部门和工资。

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

class worker
{
public:
	string name;
	int salary;
};
int main(void)
{
	srand((unsigned int)time(NULL));

	vector<worker> v;
	worker w1;
	w1.name = "Linda";
	w1.salary = rand() % 5000 + 5000 + 1;
	worker w2;
	w2.name = "Larry";
	w2.salary = rand() % 5000 + 5000 + 1;

	worker w3;
	w3.name = "Lester";
	w3.salary = rand() % 5000 + 5000 + 1;
	worker w4;
	w4.name = "Lisa";
	w4.salary = rand() % 5000 + 5000 + 1;
	worker w5;
	w5.name = "Lidia";
	w5.salary = rand() % 5000 + 5000 + 1;

	worker w6;
	w6.name = "Lee";
	w6.salary = rand() % 5000 + 5000 + 1;
	worker w7;
	w7.name = "Lane";
	w7.salary = rand() % 5000 + 5000 + 1;
	worker w8;
	w8.name = "Leslie";
	w8.salary = rand() % 5000 + 5000 + 1;

	worker w9;
	w9.name = "Lorena";
	w9.salary = rand() % 5000 + 5000 + 1;
	worker w10;
	w10.name = "Lorenzo";
	w10.salary = rand() % 5000 + 5000 + 1;

	worker w11;
	w11.name = "Lowell";
	w11.salary = rand() % 5000 + 5000 + 1;
	worker w12;
	w12.name = "Laura";
	w12.salary = rand() % 5000 + 5000 + 1;

	v.push_back(w1);
	v.push_back(w2);
	v.push_back(w3);
	v.push_back(w4);
	v.push_back(w5);

	v.push_back(w6);
	v.push_back(w7);
	v.push_back(w8);
	v.push_back(w9);
	v.push_back(w10);

	v.push_back(w11);
	v.push_back(w12);
	multimap<int, worker> m;
	for (vector<worker>::iterator it = v.begin(); it != v.end(); ++it) {
		int work;
		work = rand() % 3;
		m.insert(make_pair(work, *it));
	}

	for (multimap<int, worker>::iterator it = m.begin(); it != m.end(); ++it) {
		if (it->first == 0) {
			cout << "A:" << "   ";
		}
		else if (it->first == 1) {
			cout << "B:" << "   ";
		}
		else if (it->first == 2) {
			cout << "C:" << "   ";
		}

		cout << "the name is " << it->second.name << "   " << "the salary is " << it->second.salary << endl;
	}
	return 0;
}

程序创建了worker类,其中name表示姓名,salary表示工资。随后创建了十二个worker类对象,并指定了姓名和工资。这里姓名是手动输入的,所以代码长了。工资是随机生成的,控制使得生成随机数的值是5001-10000。随后加入vector容器v。然后创建一个multimap容器m,键表示部门(0是A,1是B,2是C),值是worker类对象。然后遍历v,对于每个类对象,都将其作为值,将生成的0-2的随机数作为键加入m中。最后遍历容器m进行输出。

下面是程序的一次运行结果:

A:   the name is Lester   the salary is 8744
A:   the name is Lisa   the salary is 9206
A:   the name is Lidia   the salary is 8034
A:   the name is Lorenzo   the salary is 9951
A:   the name is Lowell   the salary is 6045
B:   the name is Lee   the salary is 6127
B:   the name is Leslie   the salary is 6978
B:   the name is Laura   the salary is 5622
C:   the name is Linda   the salary is 8658
C:   the name is Larry   the salary is 7003
C:   the name is Lane   the salary is 9832
C:   the name is Lorena   the salary is 8370

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值