【问题标题】:C++ Randomly select from string arrayC ++从字符串数组中随机选择
【发布时间】:2017-01-12 04:43:39
【问题描述】:

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

#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”循环

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

【问题讨论】:

  • 为什么,哦,为什么,当rand 吸得这么厉害时,人们还坚持使用它。只需使用 &lt;random&gt; 的东西 -
  • 这不应该编译。在函数中,random 是循环本地的;它不应该能够在循环之外返回。在循环外声明random,然后从循环内删除类型。
  • @JesperJuhl:可能是因为&lt;random&gt; 的界面比较繁琐,而rand() 适用于大多数人的情况。
  • 另外,您需要将random 改为字符串列表,并添加到其中而不是在每个循环上覆盖。
  • @BenjaminLindley &lt;random&gt; 有一个很好的界面,分离引擎和发行版是一个明智的设计选择。

标签: c++ arrays string random


【解决方案1】:

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

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

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

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

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

#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;
}

【讨论】:

  • 这只是问题的一半。这仍然不会做他们想要的。他们想要返回 3 个值,但循环当前会在每次迭代时覆盖前一个值。
  • @Carcigenicate 你是对的。我添加你的建议。
  • 这仍然没有向控制台打印任何内容。只是一个空白控制台。
  • 你确定吗?我已经测试过了:我在帖子中添加了完整的代码。
  • @Codet 我更改了我的代码以使用 c++-98 进行编译。 auto 专用于 c++11 及更高版本。
【解决方案2】:

string random 的范围仅在代码的 for 循环内。
试试这个:

string BoxOfProduce::randomize()
 {
     srand(time(0));
     string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
     string random = "";
     for(int i = 0; i < 3; i++)
     {
         random = choices[rand() % 5];
     }
     return random;
 }

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

     BoxOfProduce bundle1;
     bundle1.randomize();
     cout << bundle1.randomize() << endl;

 }

【讨论】:

    【解决方案3】:

    您提供的代码甚至不应该编译。试试这个:

    string BoxOfProduce::randomize()
    {
        srand(time(0));
        string choices[] = {"Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo"};
        string random;    
        for(int i = 0; i < 3; i++)
        {
            random = choices[rand() % 5];
        }
        return random;
    }
    
    int main()
    {
        srand(time(0));
    
        BoxOfProduce bundle1;
        bundle1.randomize();
        cout << bundle1.randomize() << endl;
    
    }
    

    大括号{ } 定义了一个范围。在该范围之后,您在其中定义的变量将被销毁。因此,您应该在开始该范围之前定义该变量。

    可能它编译的原因是您在类BoxOfProduce 的主体内已经有另一个变量string random。因此,您要么像我一样在范围之外定义 string random,要么只删除 random 之前的 string,然后编译器将使用类主体中的变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多