map、multimap容器 常用 API操作

1 map插入访问

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

class Person {
public:
	friend void test1();
	friend void printMap(map<int,Person>&m);
	Person() {}
	Person(int num, string name, float score) {
		this->num = num;
		this->name = name;
		this->score = score;
	}
private:
	int num;
	string name;
	float score;
};
void printMap(map<int, Person>&m) {
	map<int, Person>::const_iterator it = m.begin();
	for (; it != m.end(); it++)
		cout << (*it).first << " "<<(*it).second.num<<" " << (*it).second.name << " " << (*it).second.score << endl;
}

void test1() {
	map<int, Person> m;
	//方式1
	m.insert(pair<int,Person>(1,Person(101,"andy ",86.6f)));
	//方式2(推荐)
	m.insert(make_pair(5,Person(105,"lily ",85.6f)));
	//方式3
	m.insert(map<int,Person>::value_type(2,Person(102,"tom  ",78.9f)));
	//方式4(危险)
	m[4] = Person(104,"jiery",87.2f);

	printMap(m);
}
int main() {
	test1();
	return 0;
}

在这里插入图片描述

方式4,存在键值是可以访问的;;但是访问不存在的键值,会将不存在的键加入map ;

//方式4,存在键值是可以访问的;;但是访问不存在的键值,会将不存在的键加入map 
	cout << m[4].num << " " << m[4].name << " " << m[4].score << endl;
	cout << m[9].num << " " << m[9].name << " " << m[9].score << endl;

	cout << "--------------------------" << endl;
	//从新打印map,发现m[9]已经加入了
	printMap(m);

在这里插入图片描述

map不允许有相同的键值,,但可以有相同的实值

void test2() {
	map<int, Person> m;

	//map不允许有相同的键值,,但可以有相同的实值
	m.insert(make_pair(5, Person(105, "lily ", 85.6f)));
	m.insert(make_pair(5, Person(108, "lily ", 85.6f)));
	m.insert(make_pair(6, Person(105, "lily ", 85.6f)));
	m.insert(make_pair(7, Person(105, "lily ", 85.6f)));

	printMap(m);
}

在这里插入图片描述

2 multimap 案例

公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门;
工作人员信息有: 姓名 年 龄 电话 工资等组成
通过 Multimap 进行信息的插入 保存显示 分部门显示员工信息 显示全部员工信息。

#define SALE_DEPATMENT 1 //销售部门 
#define DEVELOP_DEPATMENT 2 //研发部门
#define FINACIAL_DEPATMENT 3 //财务部门 
#define ALL_DEPATMENT 4 //所有部门

完整代码

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

//创建员工类
class Employee {
public:
	friend void printMulimap(multimap<int, Employee>& m);
	friend void createEmployee(vector<Employee>& v);
	friend void employeeJoinDepartment(vector<Employee>& v, multimap<int, Employee>& m);
	friend void showDepartmentEmployee(multimap<int, Employee>& m);

	Employee() {}
	Employee(string name,int age, string tel, int salary) {
		this->name = name;
		this->age = age;
		this->tel = tel;
		this->salary = salary;
	}
private:
	string name;
	int age;
	string tel;
	int salary;
};

void printMulimap(multimap<int, Employee>& m) {
	multimap<int, Employee>::const_iterator it = m.begin();
	for (; it != m.end(); it++)
		cout << (*it).first << " " << (*it).second.name << " " << (*it).second.age << " " << (*it).second.tel <<" "<<(*it).second.salary<<endl;
}
//创建5名员工,存放在vector
void createEmployee(vector<Employee>&v) {
	//Employee p1("zhang", 30," 123456789", 5000);
	//Employee p2("zhao ", 26, "123456789", 15000);
	//Employee p3("wang ", 28, "123456789", 8000);
	//Employee p4("liu  ", 26, "123456789", 12000);
	//Employee p5("sun  ", 27, "123456789", 9000);

	//v.push_back(p1);
	//v.push_back(p2);
	//v.push_back(p3);
	//v.push_back(p4);
	//v.push_back(p5);
	

	//设置随机数种子
	srand(time(NULL));
	for (int i = 0; i < 5; i++) {
		string seedName = "ABCDE";
		string tmpName = "员工";
		tmpName += seedName[i];
		int age = 20 + i;
		int salary = 15000 + rand() % 10 * 1000;
		string tel = to_string(rand());

		v.push_back(Employee(tmpName,age,tel,salary));
	}
}
//5名员工加入部门
void employeeJoinDepartment(vector<Employee>& v, multimap<int, Employee> &m) {
	//m.insert(make_pair(1, v[0]));
	//m.insert(make_pair(1, v[1]));
	//m.insert(make_pair(2, v[2]));
	//m.insert(make_pair(2, v[3]));
	//m.insert(make_pair(3, v[4]));

	vector<Employee>::iterator it = v.begin();
	for (; it != v.end(); it++) {
		//*it = Employee
		cout << "请输入 【" <<(*it).name<<"】加入的部门:1 销售 2 研发 3 财务 "<< endl;
		int op;
		cin >> op;
		m.insert(make_pair(op,*it));
	}
}
//显示部门员工
void showDepartmentEmployee(multimap<int, Employee>& m) {
	cout << "请输入部门:\n1 销售\n2 研发\n3 财务\n4 全部" << endl;
	int op;
	cin >> op;
	switch (op) {
		case 1:
			cout << "销售部门员工信息如下:" << endl;
			break;
		case 2:
			cout << "研发部门员工信息如下:" << endl;
			break;
		case 3:
			cout << "财务部门员工信息如下:" << endl;
			break;
		case 4:
			cout << "所有部门员工信息如下:" << endl;
			printMulimap(m);
			break;
	}
	//寻找op的位置	
	multimap<int, Employee>::iterator ret;
	ret = m.find(op);
	if (ret == m.end()) return;

	//统计op的个数
	int count = m.count(op);
	//从op的位置 按照个数 逐个变量

	for (int i= 0; i < count; i++, ret++) {
		cout << (*ret).first << " " << (*ret).second.name << " " << (*ret).second.age << " " << (*ret).second.tel << " " << (*ret).second.salary << endl;
	}
}

//接口测试
void test1() {
	//创建5名员工,存放在vector
	vector<Employee> v;
	createEmployee(v);

	//5名员工加入部门
	multimap<int, Employee> m;
	employeeJoinDepartment(v,m);

	//显示部门员工
	showDepartmentEmployee(m);
}
int main(){
	test1();
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

R-G-B

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

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

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

打赏作者

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

抵扣说明:

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

余额充值