C++ 从字符串数组中随机选择 [英] C++ Randomly select from string array

查看:214
本文介绍了C++ 从字符串数组中随机选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个函数,随机选择五个字符串中的三个并将它们显示在屏幕上.这是我到目前为止所拥有的.它运行但没有打印到屏幕上.

I'm trying to devise a function that randomly selects three of the five strings and displays them on the screen. Here's what I have so far. It runs but nothing prints to the screen.

#include "BoxOfProduce.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>


using namespace std;

BoxOfProduce::BoxOfProduce()
:choices{""}, bundles{""}
{

}

vector<string> BoxOfProduce::randomize()
{
    srand(time(0));
    string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};

    vector<string> random;
    for(int i = 0; i < 3; i++)
    {
        random.push_back(choices[rand() % 5]);
    }
    return random;
}


#ifndef BOXOFPRODUCE_H
#define BOXOFPRODUCE_H
#include <iostream>
#include <string>
#include <vector>
#include <memory>

using namespace std;


class BoxOfProduce
{
    public:
        BoxOfProduce();
        string getBundles();
        void setBundles(string b);
        vector<string> randomize();

    private:
        string bundles[3];
        const string choices[5];
        string random;
};

#endif // BOXOFPRODUCE_H


#include <iostream>
#include "BoxOfProduce.h"
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
#include <memory>

using namespace std;

int main()
{
    srand(time(0));

    BoxOfProduce bo;
    bo.randomize();

    auto vector<string> randomResult = bo.randomize();
    for (const auto& result : randomResult){
      cout << result << endl;
    }
}

我现在已经更新了我的代码,但仍然没有打印输出.虽然我收到一个错误:错误:C++98 模式下不允许基于范围的for"循环

I have updated my code now and still no print output. Although I am getting an error: error: range-based 'for' loops are not allowed in C++98 mode

我以前从未使用过 auto.因此,我们将不胜感激.

I have never worked with auto before. So any help on this would be appreciated.

推荐答案

您的代码不应编译.g++ 发出以下错误:

Your code should not compile. g++ emits the following error:

return random;
error: could not convert ‘random’ from ‘long int (*)()throw ()’

random 变量是 for-body 的本地变量.你应该给它更大的范围:

The random variable is local to your for-body. You should give it a greater scope:

string random;
for(int i = 0; i < 3; i++)
{
    random = choices[rand() % 5];
}
return random;

要产生 3 个结果,您需要返回一个字符串向量,如

To produce the 3 results, you need to return a vector of string like

#include<ctime>
#include<cstdlib>
#include<string>
#include<memory>
#include<vector>
#include<iostream>
using namespace std;

std::vector<string> randomize()
{
    srand(time(0));
    string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};

    std::vector<string> random;
    for(int i = 0; i < 3; i++)
    {
        random.push_back(choices[rand() % 5]);
    }
    return random;
}

int main()
{
    srand(time(0));

    randomize();
    std::vector<string> randomResult = randomize();
    for (std::vector<string>::const_iterator iter = randomResult.begin(), iterEnd = randomResult.end();
           iter != iterEnd; ++iter)
      cout << *iter << endl;
    return 0;
}

这篇关于C++ 从字符串数组中随机选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆