C++ development of generic programming stage program [employee grouping]

1. Description

The company recruited 10 employees (ABCDEFGHIJ). After 10 employees enter the company, they need to be assigned to work in that department.

Employee information includes: name and salary composition;

The departments are divided into: planning, art, research and development

Features:

Randomly assign department and salary to 10 employees

Insert information through multimap key (department number) value (employee)

Display employee information by department

Two, ideas

First, we need to create 10 employees and put them in the vector,

Then traverse the vector container, take out each employee, and group them randomly,

After grouping, put the employee department number as the key, and the specific employee as the value, and put it into the multimap container.

Finally, use multimap to display employee information by department.

Three, the code

First look at the main function logic:

 void test01()
{
	 //创建随机数种子
	 srand((unsigned)time(NULL));
	//1.创建员工
	 vector<Worker> vWorker;
	 createWorker(vWorker);
	 showWorker(vWorker);
	 //2.员工分组
	 //创建员工组
	 multimap<int, Worker> mWorker;
	 //分组
	 setGroup(vWorker,mWorker);
	 //显示分组员工
	 showWorkerByGroup(mWorker);
}

Create a vector container for employees:

void createWorker(vector<Worker>& v)
{
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0; i < 10; i++)
	{
		Worker worker;
		worker.name = "员工";
		worker.name += nameSeed[i];
		worker.salary = rand() % 10000 + 10000;
		v.push_back(worker);
	}
}

Show employees:

void showWorker(const vector<Worker>& v)
{
	for (vector<Worker>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << it->name << " 工资:" << it->salary << endl;
	}
}

Separate Group: 

void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		//产生随即部门编号
		int deptId = rand() % 3;
		//将员工插入到分组中
		//key部门编号,value具体员工
		m.insert(make_pair(deptId,*it));
	}
}

 Display group:

void showWorkerByGroup(multimap<int,Worker>& m)
{
	cout << "策划部门:" << endl;
	multimap<int,Worker>::iterator pos = m.find(CEHUA);
	int count = m.count(CEHUA);
	int index = 0;
	for (; pos != m.end()&&index<count; pos++,index++)
	{
		cout << "姓名:" << pos->second.name;
		cout << " 工资:" << pos->second.salary<<endl;
	}
	cout << "----------------------" << endl;
	cout << "美术部门:" << endl;
	count = m.count(MEISHU);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名:" << pos->second.name;
		cout << " 工资:" << pos->second.salary << endl;
	}
	cout << "----------------------" << endl;
	cout << "研发部门:" << endl;
	count = m.count(YANFA);
	index = 0;
	for (; pos != m.end() && index < count; pos++, index++)
	{
		cout << "姓名:" << pos->second.name;
		cout << " 工资:" << pos->second.salary << endl;
	}
}

Here index is a record value, used to end the loop. Initialized to 0, and then the condition for the end of the loop is index<count of the corresponding department.

Run screenshot:

Guess you like

Origin blog.csdn.net/Kukeoo/article/details/114097655