一个员工分组的案例,最后输出能不能用分组后输出工资的大小进行排序,具体应该怎么操作呢??试过用仿函数去实现它,但是又不知道类名应该放在何处,求解答!
具体案例和代码及测试结果如下所示:
1、案例描述:
招聘10名员工(A、B、C、D、E、F、G、H、I、J),10名员工进入公司之后,需要指派员工在哪个部门工作;
员工信息有:姓名、工资组成;
部门分为:策划、美术、研发;
随机给10名员工分配部门和工资;
通过multimap进行信息的插入key(部门编号)和value(员工);
分部门显示员工信息。
2、实现方式:
创建10名员工,放到vector中;
遍历vector容器,取出每个员工,进行随机分组;
分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中;
分部门显示员工信息。
#include
#include
#include
#include
constexpr auto MASTERMIND = 0;
constexpr auto Arts_Department = 1;
constexpr auto scientific_department = 2;
using namespace std;
class Work
{
public:
Work(string name, int wages)
{
m_name = name;
m_wages = wages;
}
string m_name;
int m_wages;
};
//仿函数重构排序规则,不知道写的对不对,类名应该添加在哪里
//class re_Sort
//{
// bool operator()(const Work &t1,const Work &t2)const
// {
// return (t1.m_wages > t2.m_wages);
// }
//};
//员工
void employee(vector &v)
{
string emy = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
string emname = "员工";
emname += emy[i];
int wage = rand() % 10000 + 10000;;
Work w(emname, wage);
v.push_back(w);
}
}
//分组
void Group(vector& v, multimap<int, Work>& mul)
{
for (vector::iterator it = v.begin(); it != v.end(); it++)
{
int pose = rand() % 3;
mul.insert(make_pair(pose, *it));
}
}
//分部门
void showCourse(multimap<int, Work>& m)
{
cout << "策划部门:" << endl;
multimap<int, Work>::iterator pos = m.find(MASTERMIND);
int count = m.count(MASTERMIND);
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_name << '\t' << "工资:" << pos->second.m_wages << endl;
}
cout << endl;
cout << "美术部门:" << endl;
pos = m.find(Arts_Department);
count = m.count(Arts_Department);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_name << '\t' << "工资:" << pos->second.m_wages << endl;
}
cout << endl;
cout << "研发部门:" << endl;
pos = m.find(scientific_department);
count = m.count(scientific_department);
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名:" << pos->second.m_name << '\t' << "工资:" << pos->second.m_wages << endl;
}
}
int main()
{
srand((unsigned int)time(nullptr));
vector w;
employee(w);
multimap<int, Work> m;
Group(w,m);
showCourse(m);
return 0;
}
测试效果:
望采纳
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Employee
{
string name;
int salary;
};
struct CompareEmployee
{
bool operator()(const Employee &e1, const Employee &e2)
{
return e1.salary < e2.salary;
}
};
int main()
{
// 创建10名员工
vector<Employee> employees;
for (int i = 0; i < 10; i++)
{
Employee e;
e.name = "E" + to_string(i);
e.salary = rand() % 1000;
employees.push_back(e);
}
// 使用仿函数对员工按工资从小到大排序
sort(employees.begin(), employees.end(), CompareEmployee());
// 输出员工信息
for (const auto &e : employees)
{
cout << e.name << " " << e.salary << endl;
}
return 0;
}