鱼C论坛

 找回密码
 立即注册
查看: 893|回复: 1

[已解决]multimap的统计问题

[复制链接]
发表于 2022-4-12 18:07:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #include<vector>
  5. #include<map>

  6. #define CEHUA 0
  7. #define MEISHU 1
  8. #define YANFA 2


  9. /*1. 创建10名员工,放到vector中
  10. 2. 遍历vector容器,取出每个员工,进行随机分组
  11. 3. 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
  12. 4. 分部门显示员工信息
  13. */

  14. class Person
  15. {
  16. public:
  17.         string p_name;
  18.         int p_salary;
  19. };

  20. void setPerson(vector<Person>&v)
  21. {
  22.         string nameSeed = "ABCDEFGHIJ";
  23.         for (int i = 0; i < 10; i++)
  24.         {
  25.                 Person worker;
  26.                 worker.p_name = "员工" ;
  27.                 worker.p_name += nameSeed[i];
  28.                 worker.p_salary = rand() % 10000 + 10000;
  29.                 v.push_back(worker);
  30.         }

  31. }


  32. void setGroup(vector<Person>&v, multimap<int, Person>&mw)
  33. {
  34.         for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
  35.         {
  36.                 int depId = rand() % 3;//0  1  2
  37.                
  38.                 //将员工插入部门中
  39.                 mw.insert(make_pair(depId, *it));
  40.         }
  41. }

  42. void showWorkerByGroup(multimap<int, Person>& mw)
  43. {
  44.         cout << "策划部门:" << endl;
  45.         multimap<int,Person>::iterator pos= mw.find(CEHUA);
  46.         int count = mw.count(CEHUA);
  47.         int index = 0;
  48.         for (; pos != mw.end() && index<count; pos++, index++)
  49.         {
  50.                 cout << "姓名:" << pos->second.p_name << " 工资:" << pos->second.p_salary << endl;
  51.         }

  52.         /*for (; pos != mw.end() && cout>0; pos++, cout--)
  53.         {
  54.                 cout << "姓名:" << pos->second.p_name << " 工资:" << pos->second.p_salary << endl;
  55.         }*/
  56.         //想问一下这个count为什么不可以--和判断,给我的感觉是数据类型不一样,我也转到multimap.find的定义看了,好像返回的数据类型确实不是int,但是我看这里又可以用int接受,就有点不理解,希望大佬们可以帮忙解释一下
  57.        
  58.        
  59.        
  60. }

  61. int main()
  62. {
  63.         vector<Person>v;
  64.         //插入员工
  65.         setPerson(v);

  66.         //员工分组
  67.         multimap<int, Person>mw;
  68.         setGroup(v, mw);

  69.         //分组显示员工
  70.         showWorkerByGroup(mw);

  71.        
  72.         system("pause");

  73.         return 0;

  74. }
复制代码
最佳答案
2022-4-12 18:43:34
cout>0; pos++, cout--)
count
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-12 18:43:34 | 显示全部楼层    本楼为最佳答案   
cout>0; pos++, cout--)
count
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 17:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表