代码
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<ctime>
#include<cstdlib>//一大堆头文件
using namespace std;
class worker{
public:
worker(char n, int p, string g){//赋值构造函数
name = n;
pay = p;
gw = g;
}
char name;//名字(以大写字母做代号)
int pay;//月薪
string gw;//岗位,分为策划、美术、研发
};
class so{//作为工具人的仿函数类
public:
bool operator()(const worker w1, const worker w2)const{
return w1.pay > w2.pay;//依照工人月薪降序排序
}
};
int main(){
srand((unsigned int)time(NULL));//随机数种子
vector<worker> w;//总工人表
vector<string> s;//岗位表
s.push_back("策划");
s.push_back("美术");
s.push_back("研发");
char a = 65;//初始值为A,最高到J
for (int i = 0; i < 10; i++){
worker p(a,rand()%10000+2000, s.at(rand()%3));
w.push_back(p);
a++;
}//随机放进工资(最低2000,最高12000,可自行改动)、岗位,
vector<worker> c, m, y;//分组为策划、美术、研发
for (vector<worker>::iterator it = w.begin(); it != w.end(); it++){//放入数据
if ((*it).gw.compare("策划") == 0){//compare即为将两个字符串比较,全等返回0
c.push_back(*it);
}
else if ((*it).gw.compare("美术") == 0){
m.push_back(*it);
}
else{
y.push_back(*it);
}
}
set<worker, so> h, u, f;//set排序
for (int i = 0; i < c.size(); i++)
{
h.insert(c[i]);
}
for (int i = 0; i < m.size(); i++)
{
u.insert(m[i]);
}
for (int i = 0; i < y.size(); i++)
{
f.insert(y[i]);
}
//逐个插入
for (set<worker,so>::iterator it = h.begin(); it != h.end(); it++)
{
cout << "名字:" << (*it).name << " 岗位:" << (*it).gw << " 工资:" << (*it).pay << endl;
}
for (set<worker,so>::iterator io = u.begin(); io != u.end(); io++)
{
cout << "名字:" << (*io).name << " 岗位:" << (*io).gw << " 工资:" << (*io).pay << endl;
}
for (set<worker,so>::iterator iy = f.begin(); iy != f.end(); iy++)
{
cout << "名字:" << (*iy).name << " 岗位:" << (*iy).gw << " 工资:" << (*iy).pay << endl;
}//逐个输出
return 0;
}
|