STL grouping of employees Case (map and vector container)

Disclaimer: This case from the B station
// employee groups _map.cpp: This file contains the "main" function. Program execution will begin and end here.
//
Here Insert Picture Description
// contains only the name, salary two properties

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

enum departMent{,,};**//用甲乙丙表示三个部门,其值分别为0,1,2**

class Worker                 //工人类,包含姓名,工资
{
public:
	int pay;
	string name;
};

void initWork(vector<Worker>& v)    //初始化工人的两个属性
{
	Worker w;
	for (int i=0; i < 5; i++)
	{
		w.name =(65+i);
		w.pay = rand() % 10000 + 10000;
		v.push_back(w);
	}
}

void setGroup(vector<Worker>& v, multimap<int, Worker>& m)//
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		int num = rand() % 3;
		m.insert(make_pair(num, *it));//multimap的插入值的方式
	}
}

**void showGroup(multimap<int, Worker>& m)//关键部分
{
	for (int i = 0; i < 3; i++)//用一个for循环可以避免写三次重复的内容
	{
		cout <<departMent(i)<< "部门人员如下" << endl;
		multimap<int, Worker>::iterator it = m.find(i);//find方法,找到了i就返回它的迭代器,没找到就                        返回end的值
		int num = m.count(i);//找出第i个部门有几个人,赋给num
		int index = 0;
		for (; it != m.end(), index < num; it++, index++)//for循环两个条件
		{
			cout << "姓名" << it->second.name << "工资" << it->second.pay << endl;
			continue;
		}
	}
}**

int main()
{
	srand((unsigned)time(NULL));
	//存放员工个人信息
	vector<Worker> v;
	//初始化员工
	initWork(v);
	//存放分组信息
	multimap<int,Worker> m;
	//分组
	setGroup(v, m);
	//输出
	showGroup(m);
	return 0;
}
Released four original articles · won praise 0 · Views 161

Guess you like

Origin blog.csdn.net/qq_45596525/article/details/104879362