鱼C论坛

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

[已解决]random_shuffle问题

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

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

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

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

speechManager.h
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include"speak.h"
#include<algorithm>

class SpeechManager
{
public:
        SpeechManager();

        //显示菜单
        void showMenu();

        //退出菜单
        void Exit();

        ~SpeechManager();

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

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

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

        //抽签
        void speechDraw();

        //保存第一轮比赛选手编号容器
        vector<int>v1;
        
        //保存第二轮比赛选手编号容器
        vector<int>v2;
        
        //胜出前三名选手编号容器
        vector<int>vVictory;

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

        //比赛轮数
        int m_index;

};

speak.h
#pragma once
#include<iostream>
using namespace std;
#include<string>


//选手类
class Speaker
{
public:

        string m_Name;
        double m_Score[2];//最多两轮得分
};

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

int main()
{
        SpeechManager sm;

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

        int choice = 0;

        while (true)
        {
                sm.showMenu();

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

                switch (choice)
                {
                case 1://开始比赛
                        break;
                case 2://查看往届记录
                        break;
                case 3://清空比赛记录
                        break;
                case 0:sm.Exit();//退出系统
                        break;
                default:
                        cout << "输入有误,请重新输入" << endl;
                        system("pause");
                        system("cls");
                        break;
                }

        }



        system("pause");

        return 0;
}

speechManager.cpp
#include<iostream>
using namespace std;
#include<string>
#include"speechManager.h"



SpeechManager:: SpeechManager()
{
        this->initSpeech();
        this->createSpeaker();
}


void SpeechManager::showMenu()
{
        cout << "******************************" << endl;
        cout << "*****  欢迎参加演讲比赛  *****" << endl;
        cout << "*****  1、开始演讲比赛   *****" << endl;
        cout << "*****  2、查看往届记录   *****" << endl;
        cout << "*****  3、清空比赛记录   *****" << endl;
        cout << "*****  0、退出比赛程序   *****" << endl;
        cout << "******************************" << endl;
}

void SpeechManager::Exit()
{
        cout << "欢迎下次使用" << endl;
        system("pause");
        exit(0);
        
}

void SpeechManager::initSpeech()
{
        //容器先置空
        this->v1.clear();
        this->v2.clear();
        this->vVictory.clear();
        this->m_Speaker.clear();

        this->m_index = 1;
}

void SpeechManager::createSpeaker()
{
        string nameSeed = "ABCDEFGHIJKL";

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

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

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

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

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

void SpeechManager:: strarSpeech()
{

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

        //抽签

        //比赛

        //显示晋级结果
        // 
        //第二轮开始比赛

        //抽签

        //比赛

        //显示最终结果


        
}

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

        if (this->m_index == 1)
        {        
                random_shuffle(v1.begin(), v1.end());                                              //包含了<algorothm>random_shuffle还是未定义
        }
        else
        {
                
        }


}


SpeechManager::~SpeechManager()
{

}
1.png



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

void SpeechManager::speechDraw()
{
    static std::random_device rd;
    static std::mt19937 re(rd());

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

    if (this->m_index == 1)
    {
        shuffle(v1.begin(), v1.end(), re);                                             
    }
    else
    {

    }


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

void SpeechManager::speechDraw()
{
    static std::random_device rd;
    static std::mt19937 re(rd());

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

    if (this->m_index == 1)
    {
        shuffle(v1.begin(), v1.end(), re);                                             
    }
    else
    {

    }


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 18:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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