STLvector和map容器案例
3.10.1案例描述
3.10.2实现步骤
代码示例:
#include
#include
#include
#include
#include
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
using namespace std;
class Worker
{
public:string m_Name;int m_Salary;
};
void createWorker(vector<Worker>&v)
{string nameSeed &#61; "ABCDEFGHIJ";for (int i &#61; 0; i < 10; i&#43;&#43;){Worker worker;worker.m_Name &#61; "员工";worker.m_Name &#43;&#61; nameSeed[i];worker.m_Salary &#61; rand() % 10000 &#43; 10000; v.push_back(worker);}
}
void setGroup(vector<Worker>&v, multimap<int, Worker>&m)
{for (vector<Worker>::iterator it &#61; v.begin(); it !&#61; v.end(); it&#43;&#43;){int deptId &#61; rand() % 3; m.insert(make_pair(deptId, *it));}
}
void showWorkerByGourp(multimap<int, Worker>&m)
{cout << "策划部门&#xff1a;" << endl;multimap<int, Worker>::iterator pos &#61; m.find(CEHUA);int count &#61; m.count(CEHUA); int index &#61; 0;for (; pos !&#61; m.end() && index < count; pos&#43;&#43;, index&#43;&#43;){cout << "姓名&#xff1a; " << pos->second.m_Name << " 工资&#xff1a;" << pos->second.m_Salary << endl;}cout << "------------------------------------" << endl;cout << "美术部门&#xff1a;" << endl;pos &#61; m.find(MEISHU);count &#61; m.count(MEISHU); index &#61; 0;for (; pos !&#61; m.end() && index < count; pos&#43;&#43;, index&#43;&#43;){cout << "姓名&#xff1a; " << pos->second.m_Name << " 工资&#xff1a;" << pos->second.m_Salary << endl;}cout << "------------------------------------" << endl;cout << "研发部门&#xff1a;" << endl;pos &#61; m.find(YANFA);count &#61; m.count(YANFA); index &#61; 0;for (; pos !&#61; m.end() && index < count; pos&#43;&#43;, index&#43;&#43;){cout << "姓名&#xff1a; " << pos->second.m_Name << " 工资&#xff1a;" << pos->second.m_Salary << endl;}
}
int main()
{srand((unsigned int)time(NULL));vector<Worker>vWorker;createWorker(vWorker);multimap<int, Worker>mWorker;setGroup(vWorker, mWorker);showWorkerByGourp(mWorker);