Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;_C++ - Fatal编程技术网

C++ 如何在c++;

C++ 如何在c++;,c++,C++,我正在制作一个程序,每次运行该程序时,都会显示一个随机引用。这应该通过使用rand和srand来完成。我在做逻辑和搜索,但不知道怎么做。谁能告诉我怎么了 const string Quot[14] = { "1)Love Pakistan", "2)Be Honest", "3)Work Work and Work", "4)I am always doing things I cannot do.That is how I get to do them.", "5)It is not

我正在制作一个程序,每次运行该程序时,都会显示一个随机引用。这应该通过使用
rand
srand
来完成。我在做逻辑和搜索,但不知道怎么做。谁能告诉我怎么了

    const string Quot[14] = { "1)Love Pakistan", "2)Be Honest", "3)Work Work and Work", "4)I am always doing things I cannot do.That is how I get to do them.", "5)It is not what we take up, but what we give up, that makes us rich.", "6)You can do anything, but not everything.", "7)Thinking will not overcome fear but action will. ", "8)We read the world wrong and say that it deceives us.", "9)You miss 100 percent of the shots you never take.", "10)He is the happiest, be he king or peasant, who finds peace in his home.", "11)Your work is to discover your work and then, with all your heart, to give yourself to it.", "12)In order to be effective truth must penetrate like an arrow – and that is likely to hurt", "13)You must be the change you wish to see in the world", "14)Humans are satisfied with whatever looks good; ? Heaven probes for what is good." };
    for (int i = 0; i < 14; i++)
    {   
        int choiceLen[i] = c.getLenght(Quot[i]);
        const int randomLength = 1;
        string randomStr[randomLength + 1];
        for (int i = 0; i < randomLength; i++)
        {
            randomStr[i] = Quot[i][rand() % choiceLen[i]];
            cout << randomStr[i] << endl;
        }
    }
const string Quot[14]={“1)爱巴基斯坦”,“2)诚实”,“3)工作,工作”,“4)我总是做我不能做的事情。这就是我如何去做的。”,“5)让我们富有的不是我们接受的,而是我们放弃的。”,“6)你可以做任何事情,但不是所有事情。”,“7)思考不会克服恐惧,但行动会。”,“8)我们看错了世界,说它欺骗了我们。”,“9)你错过了100%你从来没有拍过的镜头。”,“10)他是最幸福的,无论是国王还是农民,只要他在家里找到了安宁。”,“11)你的工作就是发现你的工作,然后全身心投入其中。”,“12) 为了有效,真理必须像箭一样穿透,这很可能会造成伤害”,“13)你必须成为你希望在世界上看到的改变”,“14)人类对任何美好的事物都感到满意;?天堂探索美好的事物。”;
对于(int i=0;i<14;i++)
{   
int choiceLen[i]=c.getLenght(Quot[i]);
常数int randomLength=1;
字符串randomStr[randomLength+1];
for(int i=0;icout
rand
是一个伪随机数生成器。这意味着它不是真正的随机数,第一个限制是,出于基于可测试性的遗留原因,它总是从相同的种子开始,因此总是产生相同的随机序列

为了打破这一点,您需要提供一些熵,一个随机种子

警告:如果在同一秒内运行两次,它将得到相同的随机种子

如果您有可用的C++11,则可以使用
std::shuffle

#include <iostream>
#include <string>
#include <array>
#include <random>
#include <algorithm>

int main()
{
    std::array<std::string, 3> quotes = {
        "1 hello", "2 world", "3 hello world"
    };

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> quoteSeed(0, quotes.size() - 1);

    int quoteNo = quoteSeed(gen);
    auto quote = quotes[quoteNo];
    std::shuffle(quote.begin(), quote.end(), gen);
    std::cout << quote << "\n";
}
#包括
#包括

#包含

只是一个旁注:始终使用同一种子是否是一个好主意取决于用例。在科学计算中,通常更重要的是拥有一个尽可能不相关的数字序列,因为有些种子比其他种子的结果更好。因此,人们经常使用“已验证且正确的”"这类用例的种子。什么是种子,此程序给我这些错误警告1 C4244:“参数”:从“time_t”转换为“unsigned int”,可能会丢失数据错误2错误C2057:预期的常量表达式错误3错误C2466:无法分配常量大小为0的数组错误4错误C2664:“size\u t strlen(const char*)”:无法将参数1从“const std::string”转换为“const char*”。不要在
std::string上使用
strlen
,使用
.length()
。例如
std::string x=“hello”
长度是
x.length()
而不是
strleng(x)如果你不熟悉std::string的基本用法,我建议你在尝试更高级的东西之前先仔细阅读一下。
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <algorithm>

int main()
{
    std::array<std::string, 3> quotes = {
        "1 hello", "2 world", "3 hello world"
    };

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> quoteSeed(0, quotes.size() - 1);

    int quoteNo = quoteSeed(gen);
    auto quote = quotes[quoteNo];
    std::shuffle(quote.begin(), quote.end(), gen);
    std::cout << quote << "\n";
}