c++ 如何在 cout 之前洗牌/混合字符串

c++ How to shuffle/mix a string before cout?

本文关键字:混合 字符串 cout c++      更新时间:2023-10-16

请参考以下代码

random_shuffle(cq.begin(), cq.end());
cout << cq ;

据我了解,我已经将两根字符串cq连接到一个cq。然后我想在cout之前洗牌/混合它.我该怎么做?

提前感谢您的回答

#include <iostream>
#include <random>
#include <algorithm>
int main()
{
   std::string str = "StackOverflow";
   std::random_device rd;
   std::mt19937 g(rd());
   std::shuffle(str.begin(), str.end(), g);
   std::cout << str.c_str() << std::endl;
   return 0;
}

您可以阅读有关 http://en.cppreference.com/w/cpp/algorithm/random_shuffle 的更多详细信息