鱼C论坛

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

[已解决]random_shuffle问题

[复制链接]
发表于 2022-4-16 16:50:19 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 孤世星辰 于 2022-4-16 16:58 编辑

speechManager.h
  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. #include<string>
  5. #include<vector>
  6. #include<map>
  7. #include"speak.h"
  8. #include<algorithm>

  9. class SpeechManager
  10. {
  11. public:
  12.         SpeechManager();

  13.         //显示菜单
  14.         void showMenu();

  15.         //退出菜单
  16.         void Exit();

  17.         ~SpeechManager();

  18.         //初始化容器和属性
  19.         void initSpeech();

  20.         //创建12名选手
  21.         void createSpeaker();

  22.         //比赛流程函数
  23.         void startSpeech();

  24.         //抽签
  25.         void speechDraw();

  26.         //保存第一轮比赛选手编号容器
  27.         vector<int>v1;
  28.        
  29.         //保存第二轮比赛选手编号容器
  30.         vector<int>v2;
  31.        
  32.         //胜出前三名选手编号容器
  33.         vector<int>vVictory;

  34.         //存放编号及对应具体选手容器
  35.         map<int, Speaker>m_Speaker;

  36.         //比赛轮数
  37.         int m_index;

  38. };
复制代码


speak.h
  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. #include<string>


  5. //选手类
  6. class Speaker
  7. {
  8. public:

  9.         string m_Name;
  10.         double m_Score[2];//最多两轮得分
  11. };
复制代码


演讲比赛流程管理系统.cpp
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #include"speechManager.h"

  5. int main()
  6. {
  7.         SpeechManager sm;

  8.         /*for (map<int, Speaker>::iterator it = sm.m_Speaker.begin(); it != sm.m_Speaker.end(); it++)
  9.         {
  10.                 cout << "选手编号:" << it->first << "选手姓名:" <<it->second.m_Name<< endl;
  11.         }*/

  12.         int choice = 0;

  13.         while (true)
  14.         {
  15.                 sm.showMenu();

  16.                 cout << "请输入您的选择" << endl;
  17.                 cin >> choice;

  18.                 switch (choice)
  19.                 {
  20.                 case 1://开始比赛
  21.                         break;
  22.                 case 2://查看往届记录
  23.                         break;
  24.                 case 3://清空比赛记录
  25.                         break;
  26.                 case 0:sm.Exit();//退出系统
  27.                         break;
  28.                 default:
  29.                         cout << "输入有误,请重新输入" << endl;
  30.                         system("pause");
  31.                         system("cls");
  32.                         break;
  33.                 }

  34.         }



  35.         system("pause");

  36.         return 0;
  37. }
复制代码


speechManager.cpp
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #include"speechManager.h"



  5. SpeechManager:: SpeechManager()
  6. {
  7.         this->initSpeech();
  8.         this->createSpeaker();
  9. }


  10. void SpeechManager::showMenu()
  11. {
  12.         cout << "******************************" << endl;
  13.         cout << "*****  欢迎参加演讲比赛  *****" << endl;
  14.         cout << "*****  1、开始演讲比赛   *****" << endl;
  15.         cout << "*****  2、查看往届记录   *****" << endl;
  16.         cout << "*****  3、清空比赛记录   *****" << endl;
  17.         cout << "*****  0、退出比赛程序   *****" << endl;
  18.         cout << "******************************" << endl;
  19. }

  20. void SpeechManager::Exit()
  21. {
  22.         cout << "欢迎下次使用" << endl;
  23.         system("pause");
  24.         exit(0);
  25.        
  26. }

  27. void SpeechManager::initSpeech()
  28. {
  29.         //容器先置空
  30.         this->v1.clear();
  31.         this->v2.clear();
  32.         this->vVictory.clear();
  33.         this->m_Speaker.clear();

  34.         this->m_index = 1;
  35. }

  36. void SpeechManager::createSpeaker()
  37. {
  38.         string nameSeed = "ABCDEFGHIJKL";

  39.         for (int i = 0; i < nameSeed.size(); i++)
  40.         {
  41.                 string name = "选手";
  42.                 name += nameSeed[i];

  43.                 //创建选手
  44.                 Speaker sp;
  45.                 sp.m_Name = name;

  46.                 for (int j = 0; j < 2; j++)
  47.                 {
  48.                         sp.m_Score[j] = 0;
  49.                 }

  50.                 //创建选手编号放到v1容器中
  51.                 this->v1.push_back(i + 1001);

  52.                 //选手编号和选手信息放入map容器
  53.                 this->m_Speaker.insert(make_pair(i + 1001, sp));
  54.         }
  55. }

  56. void SpeechManager:: strarSpeech()
  57. {

  58.         cout << "开始第" << m_index << "轮比赛" << endl;
  59.         //第一轮开始比赛

  60.         //抽签

  61.         //比赛

  62.         //显示晋级结果
  63.         //
  64.         //第二轮开始比赛

  65.         //抽签

  66.         //比赛

  67.         //显示最终结果


  68.        
  69. }

  70. void SpeechManager::speechDraw()
  71. {
  72.         cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  73.         cout << "---------------------------------------" << endl;
  74.         cout << "抽签后演讲顺序为" << endl;

  75.         if (this->m_index == 1)
  76.         {       
  77.                 random_shuffle(v1.begin(), v1.end());                                              //包含了<algorothm>random_shuffle还是未定义
  78.         }
  79.         else
  80.         {
  81.                
  82.         }


  83. }


  84. SpeechManager::~SpeechManager()
  85. {

  86. }
复制代码

1.png



最佳答案
2022-4-16 18:33:23
我猜你用的是C++17吧?
C++17删去了random_shuffle,建议使用C++11中加入的shuffle函数(需结合C++11加入的随机数生成器使用):
  1. // 文件开头
  2. #include<random>

  3. void SpeechManager::speechDraw()
  4. {
  5.     static std::random_device rd;
  6.     static std::mt19937 re(rd());

  7.     cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  8.     cout << "---------------------------------------" << endl;
  9.     cout << "抽签后演讲顺序为" << endl;

  10.     if (this->m_index == 1)
  11.     {
  12.         shuffle(v1.begin(), v1.end(), re);                                             
  13.     }
  14.     else
  15.     {

  16.     }


  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-16 18:33:23 | 显示全部楼层    本楼为最佳答案   
我猜你用的是C++17吧?
C++17删去了random_shuffle,建议使用C++11中加入的shuffle函数(需结合C++11加入的随机数生成器使用):
  1. // 文件开头
  2. #include<random>

  3. void SpeechManager::speechDraw()
  4. {
  5.     static std::random_device rd;
  6.     static std::mt19937 re(rd());

  7.     cout << "第" << this->m_index << "轮选手正在抽签" << endl;
  8.     cout << "---------------------------------------" << endl;
  9.     cout << "抽签后演讲顺序为" << endl;

  10.     if (this->m_index == 1)
  11.     {
  12.         shuffle(v1.begin(), v1.end(), re);                                             
  13.     }
  14.     else
  15.     {

  16.     }


  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 16:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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