C++——STL案例

在这里插入图片描述
代码:

#include <iostream>
#include <string>
#include <vector>
#include<deque>
#include<stack>
#include<list>
#include<set>
#include<map>
#include <algorithm>
using namespace std;

#define CHEHUA 0
#define YUNYING 1
#define KAIFA 0

class work
{
public:

	string w_name;
	int w_salary;
};

//创建员工至容器中
void create_work(vector<work>& v)
{
	string name_seed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++) 
	{
		work w;
		w.w_name = "员工";
		w.w_name += name_seed[i];

		w.w_salary = rand() & 10000 + 10000;
		v.push_back(w);
	}
}

//员工分组
void set_group(vector<work>& v, multimap<int, work>& m)
{
	for (vector<work>::iterator it = v.begin(); it != v.end(); it++) 
	{
		int dep_ID = rand() % 3;
		m.insert(make_pair(dep_ID, *it));		//插入到map中
	}
}


//显示员工
void show_work(vector<work>& v)
{
	for (vector<work>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "姓名:" << (*it).w_name << " 工资:" << (*it).w_salary << endl;
	}
}

//遍历map
void show_dep(multimap <int, work>& m)
{
	cout << "----策划部----" << endl;
	multimap<int, work>::iterator pos = m.find(CHEHUA);
	int count = m.count(CHEHUA);
	int index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名:" << pos->second.w_name << " 工资:" << pos->second.w_salary << endl;
	}

	cout <<endl;
	cout << "----运营部----" << endl;
	pos = m.find(YUNYING);
	count = m.count(YUNYING);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名:" << pos->second.w_name << " 工资:" << pos->second.w_salary << endl;
	}

	cout <<endl;
	cout << "----开发部----" << endl;
	pos = m.find(KAIFA);
	count = m.count(KAIFA);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名:" << pos->second.w_name << " 工资:" << pos->second.w_salary << endl;
	}

}


int main()
{
	//srand()函数产生一个以当前时间开始的随机种子 
	srand((unsigned)time(NULL));
	
	//创建vector容器。
	vector<work> wok;
	
	//创建十名员工,加入容器中。
	create_work(wok);

	//员工分组
	multimap<int, work> mp;
	set_group(wok,mp);

	//显示
	show_dep(mp);

	system("pause");
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值