multimap容器案例以及详细讲解,附属经典容器的使用场景

  

 第一步是先建立员工的个人信息类:

class Person
{
	friend void printfm(multimap<int, Person>& m);
	friend void joinv(vector<Person>& c, multimap<int, Person>& v);
	friend void show(multimap<int, Person>& v);
private:
	string name;
	int age;
	int num;
	int salary;
public:
	Person(){}
	Person(string name, int age, int num, int salary)
	{
		this->name = name;
		this->age = age;
		this->num = num;
		this->salary = salary;
	}
};

2、第二步是建立函数体,看你需要啥样的函数

void text14()
{
	//创建5一个容器vector将5个员工信息放入
	vector<Person>c;
	pushvextor(c);
	//五名员工加入部门
	multimap<int, Person>v;
	joinv(c, v);
	//输出信息
	show(v);
}

3、将子函数逐一写出

建立一个vector容器将5个员工的信息放入
vector<Person>c;
void pushvextor(vector<Person>&c)
{
	string Name = "ABCDE";
	int i = 0;
	srand(time(NULL));
	for (i=0; i < 5; i++)
	{
		string name = "员工";
		name += Name[i];
		int age = 18 + rand() % 10;
		int num = i;
		int salary = 5000 + rand() % 3000;
		c.push_back(Person(name, age, num, salary));
	}
}
将vector里面的数据放入multimap中,并且将员工加入的部门也放入multimap.second中;
vector<Person>c;
multimap<int, Person>v;
void joinv(vector<Person>&c, multimap<int, Person>&v)
	{
	vector<Person>::iterator it = c.begin();
	for(;it!=c.end();it++)
	{
		cout << "请输入" << (*it).name << "加入的部门" << endl;
		int op = 0;
		cin >> op;
		v.insert(make_pair(op, *it));
	}
	}

void show(multimap<int, Person>& v)
{
	int op = 0;
	cout << "请输入部门编号0--4" << endl;
	cin >> op;
	
		switch (op)
		{
		case 0:
		{
			cout << "--------组织部-------" << endl;
			multimap<int, Person>::iterator it;

			it = v.find(op);
			for (; it != v.end(); it++)
				cout << (*it).second.name << "  " << (*it).second.age << "  " <<
				(*it).second.num << "  " << (*it).second.salary << endl;
			break;
		}
		case 1:
		{
			cout << "--------行政部-------" << endl;
			multimap<int, Person>::iterator it;
			it = v.find(op);
			for (; it != v.end(); it++)
				cout << (*it).second.name << "  " << (*it).second.age << "  " <<
				(*it).second.num << "  " << (*it).second.salary << endl;
			break;
		}
		case 3:
		{
			break;
		}
		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值