#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
#include <ctime>
//宏定义
#define cehua 1
#define meishu 2
#define yanfa 3
class Worker
{
public:
//Worker(string name,int salary)
//{
// this->m_Name = name;
// this->m_Salary = salary;
//}
public:
string m_Name;
int m_Salary;
};
void createWorker( vector<Worker> & v )
{
string nameseed = "ABCDEFGHIJ";
for( int i = 0;i<10; i++)
{
Worker worker;
worker.m_Name = "员工 ";
worker.m_Name += nameseed[i];
worker.m_Salary = rand()%10000 + 10000; //10000-19999
v.push_back(worker);
}
}
void setGroup( vector<Worker> & v ,multimap<int,Worker> &m)
{
for( vector<Worker>::iterator it = v.begin(); it!= v.end(); it++ )
{
int departmentid = rand()%3 + 1; // 部门 1 2 3;
m.insert(make_pair(departmentid,*it)); //插入随机部门和员工
}
}
//void showWorkerByFroup( multimap<int,Worker> & m)
//{
// for( multimap<int,Worker>:: iterator it = m.begin(); it != m.end(); it++ )
// {
// cout << "职位 " <<it->first << " "
// << "姓名 " << it->second.m_Name << " "
// << "工资 " << it->second.m_Salary << endl;
// }
//
//}
void showWorkerByFroup( multimap<int,Worker> & m)
{
//按部门输出
cout << "策划部门: " << endl;
//cout << m.find(1) << endl;
multimap<int,Worker>::iterator pos = m.find(cehua);
int num = m.count(cehua);
int index = 0;
for( ; pos != m.end() && index < num; pos++,index++ )
{
cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
}
cout << "--------------------------" << endl;
cout << "美术部门: " << endl;
pos = m.find(meishu);
num = m.count(meishu);
index = 0;
for( ; pos != m.end() && index < num; pos++,index++ )
{
cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
}
cout << "--------------------------" << endl;
cout << "研发部门: " << endl;
pos = m.find(yanfa);
num = m.count(yanfa);
index = 0;
for( ; pos != m.end() && index < num; pos++,index++ )
{
cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
}
//cout << "美术部门: " << endl;
//multimap<int,Worker>::iterator pos1 = m.find(meishu);
//int num1 = m.count(meishu);
//for(multimap<int,Worker>::iterator pos1 = m.find(meishu); pos1 != m.end() && index < num1; pos1++,index++ )
//{
// cout << pos1->second.m_Name << " " << pos1->second.m_Salary << endl;
//}
//cout << "研发部门: " << endl;
//multimap<int,Worker>::iterator pos2 = m.find(yanfa);
//int num2 = m.count(yanfa);
//for( multimap<int,Worker>::iterator pos2 = m.find(yanfa); pos2 != m.end() && index < num2; pos2++,index++ )
//{
// cout << pos2->second.m_Name << " " << pos2->second.m_Salary << endl;
//}
}
int main()
{
//随机
srand((unsigned)time(NULL));
vector<Worker> vWorker;
createWorker(vWorker);
//分组操作
multimap<int,Worker> mWorker;
setGroup(vWorker,mWorker);
//测试
//for( vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++ )
//{
// cout << it->m_Name << ""<< it->m_Salary << endl;
//}
//测试
//for( multimap<int,Worker>:: iterator it = mWorker.begin(); it != mWorker.end(); it++ )
//{
// cout << it->first << " " << it->second.m_Name << " " << it->second.m_Salary << endl;
//}
showWorkerByFroup(mWorker);
system("pause");
return 0;
}
STL代码练习
最新推荐文章于 2024-02-29 15:45:15 发布