|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<iostream>
- using namespace std;
- #include<string>
- #include<vector>
- #include<map>
- #define CEHUA 0
- #define MEISHU 1
- #define YANFA 2
- /*1. 创建10名员工,放到vector中
- 2. 遍历vector容器,取出每个员工,进行随机分组
- 3. 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
- 4. 分部门显示员工信息
- */
- class Person
- {
- public:
- string p_name;
- int p_salary;
- };
- void setPerson(vector<Person>&v)
- {
- string nameSeed = "ABCDEFGHIJ";
- for (int i = 0; i < 10; i++)
- {
- Person worker;
- worker.p_name = "员工" ;
- worker.p_name += nameSeed[i];
- worker.p_salary = rand() % 10000 + 10000;
- v.push_back(worker);
- }
- }
- void setGroup(vector<Person>&v, multimap<int, Person>&mw)
- {
- for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
- {
- int depId = rand() % 3;//0 1 2
-
- //将员工插入部门中
- mw.insert(make_pair(depId, *it));
- }
- }
- void showWorkerByGroup(multimap<int, Person>& mw)
- {
- cout << "策划部门:" << endl;
- multimap<int,Person>::iterator pos= mw.find(CEHUA);
- int count = mw.count(CEHUA);
- int index = 0;
- for (; pos != mw.end() && index<count; pos++, index++)
- {
- cout << "姓名:" << pos->second.p_name << " 工资:" << pos->second.p_salary << endl;
- }
- /*for (; pos != mw.end() && cout>0; pos++, cout--)
- {
- cout << "姓名:" << pos->second.p_name << " 工资:" << pos->second.p_salary << endl;
- }*/
- //想问一下这个count为什么不可以--和判断,给我的感觉是数据类型不一样,我也转到multimap.find的定义看了,好像返回的数据类型确实不是int,但是我看这里又可以用int接受,就有点不理解,希望大佬们可以帮忙解释一下
-
-
-
- }
- int main()
- {
- vector<Person>v;
- //插入员工
- setPerson(v);
- //员工分组
- multimap<int, Person>mw;
- setGroup(v, mw);
- //分组显示员工
- showWorkerByGroup(mw);
-
- system("pause");
- return 0;
- }
复制代码
cout>0; pos++, cout--)
count
|
|