2017-05-23 35 views
-2

請參閱下面的代碼C++如何在cout之前混洗/混合字符串?

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

據我所知,我在一個cq連接兩個字符串cq。然後我想在cout之前洗牌/混合它。我怎麼做?

感謝您的答案提前

+3

什麼是理想的結果VS你現在所得到的結果呢? –

+2

你的問題不清楚。 「將兩個字符串'c'和'q'連接到一個'cq'」是什麼意思?請注意'std :: random_shuffle()'在C++ 14中已被棄用,並從C++ 17中刪除。改用'std :: shuffle()'。 – Peter

+0

大家好,謝謝你的回答。可以說,如果我有一個字符串colled cq中的10個符號(數字和字母)。如何在cout之前混合它? – AndriusJ

回答

1
#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