-3
我的源代碼,我不能混淆我的話,它讓我得到任何我不想要的東西。C++不能混淆一個詞
我沒有錯誤,沒有警告,但舉個例子,如果我說:爸爸,它給我'P A',爲什麼? 感謝您的幫助!
#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;
}
更願意通過'常量的std :: string'傳遞字符串,這樣編譯器並不需要進行復印。 –
如果你的目標只是將字符串洗牌,那麼使用'std :: shuffle'有什麼問題? – PaulMcKenzie