C ++不能混淆一个词

时间:2023-01-13 10:40:16

Im my source code, I just can't get mixed up my word, it's getting me anything I don't want.

我的源代码,我只是不能混淆我的话,它给我任何我不想要的东西。

I have no errors, no warning but for an example if I put: papa, it's giving me ' p a ', why ? Thank's for your help !

我没有错误,没有任何警告但是作为一个例子,如果我把:爸爸,它给了我'一个',为什么?谢谢你的帮助 !

#include <iostream>
#include <string>
#include <random>
#include <chrono>

using namespace std;

string melangeLettre(string mot);
int main()
{
    cout << "Saisissez un mot mystere: \n> ";
    string motMystere{};
    cin >> motMystere;

    cout << "Quel est ce mot ? \n";
    string const newMot = melangeLettre(motMystere);
    cout << newMot << endl;

    return {0};
}

string melangeLettre(string mot)
{
    size_t random = chrono::system_clock::now().time_since_epoch().count();
    mt19937 gen{random};
    string newMot{};

    for (unsigned int i{}; i < mot.size(); ++i)
    {
        uniform_int_distribution<> getNbr(0, mot.size());
        newMot.push_back(mot[getNbr(gen)]);
        mot.erase(i, 1);
    }

    return newMot;
}

1 个解决方案

#1


You have a few problems in your code. Your choosen distribution:

您的代码中存在一些问题。您选择的分配:

uniform_int_distribution<> getNbr(0, mot.size());

can result in numbers including mot.size() which is past the end of the string. The last character in a non-empty string will have an index of mot.size()-1.

可以导致数字,包括超过字符串结尾的mot.size()。非空字符串中的最后一个字符将具有mot.size() - 1的索引。

In this code:

在这段代码中:

newMot.push_back(mot[getNbr(gen)]);
mot.erase(i, 1);

you copy a character to the new word and then delete a different character from the original word (it could be the same character but only by chance). You probably want to delete the same character you add to the new word like:

您将一个字符复制到新单词,然后从原始单词中删除一个不同的字符(它可能是相同的字符,但只是偶然)。您可能希望删除添加到新单词的相同字符,如:

auto j = getNbr(gen);
newMot.push_back(mot[j]);
mot.erase(j, 1);

Your loop iterates too few times because you are deleting characters from the word in each loop. Because of this you really just need to iterate until your original word is empty.

你的循环迭代次数太少,因为你要从每个循环中的单词中删除字符。因此,您只需要迭代直到原始单词为空。

These three things change the loop in your function to:

这三件事将你的函数循环改为:

while (mot.size() > 0)
{
    uniform_int_distribution<> getNbr(0, mot.size()-1);
    auto j = getNbr(gen);
    newMot.push_back(mot[j]);
    mot.erase(j, 1);
}

#1


You have a few problems in your code. Your choosen distribution:

您的代码中存在一些问题。您选择的分配:

uniform_int_distribution<> getNbr(0, mot.size());

can result in numbers including mot.size() which is past the end of the string. The last character in a non-empty string will have an index of mot.size()-1.

可以导致数字,包括超过字符串结尾的mot.size()。非空字符串中的最后一个字符将具有mot.size() - 1的索引。

In this code:

在这段代码中:

newMot.push_back(mot[getNbr(gen)]);
mot.erase(i, 1);

you copy a character to the new word and then delete a different character from the original word (it could be the same character but only by chance). You probably want to delete the same character you add to the new word like:

您将一个字符复制到新单词,然后从原始单词中删除一个不同的字符(它可能是相同的字符,但只是偶然)。您可能希望删除添加到新单词的相同字符,如:

auto j = getNbr(gen);
newMot.push_back(mot[j]);
mot.erase(j, 1);

Your loop iterates too few times because you are deleting characters from the word in each loop. Because of this you really just need to iterate until your original word is empty.

你的循环迭代次数太少,因为你要从每个循环中的单词中删除字符。因此,您只需要迭代直到原始单词为空。

These three things change the loop in your function to:

这三件事将你的函数循环改为:

while (mot.size() > 0)
{
    uniform_int_distribution<> getNbr(0, mot.size()-1);
    auto j = getNbr(gen);
    newMot.push_back(mot[j]);
    mot.erase(j, 1);
}