STL小案例
创建10名员工,放到vector中。遍历vector容器,取出每个员工,进行随机分组。分组后,将员工部门编号作为key,具体员工资为value,放入到multimap容器中,分部门显示员工信息。
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
class worker
{
public:
string name;
int salary;
};
void CreatePlayer(vector<worker>& a)
{
char b[] = "ABCDEFGHIJ";
int i = 0;
for (; i < 10; i++)
{
worker WORKER;
WORKER.name = b[i];
WORKER.salary = rand() % 5000 + 5000;
a.push_back(WORKER);
}
}
void MapWorker(vector<worker>& vec, multimap<int, worker>& mp)
{
int ID;
for (int i = 0; i < 10; i++)
{
pair<int, worker>temp;
ID = rand() % 3;
temp.first = ID;
temp.second = vec[i];
mp.insert(temp);
}
}
void ShowWork(multimap<int, worker>& mp)
{
for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 0)
{
cout << "策划部" << "姓名" << begin->second.name
<< "工资" << begin->second.salary << endl;
}
}
for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 1)
{
cout << "美术部" << "姓名" << begin->second.name
<< "工资" << begin->second.salary << endl;
}
}
for (map<int, worker>::iterator begin = mp.begin(); begin != mp.end(); begin++)
{
if (begin->first == 2)
{
cout << "研发部" << "姓名" << begin->second.name
<< "工资" << begin->second.salary << endl;
}
}
}
void test()
{
vector<worker>WORKERS;
CreatePlayer(WORKERS);
multimap<int, worker>mWORKERS;
MapWorker(WORKERS, mWORKERS);
ShowWork(mWORKERS);
}
int main()
{
test();
return 0;
}