本文主要是介绍【C++】algorithm--shuffle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
shuffle在algorithm当中
// shuffle algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::shuffle
#include <array> // std::array
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clockint main () {std::array<int,5> foo {1,2,3,4,5};// obtain a time-based seed:unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();shuffle (foo.begin(), foo.end(), std::default_random_engine(seed));//std::default_random_engine(seed)可以省略std::cout << "shuffled elements:";for (int& x: foo) std::cout << ' ' << x;std::cout << '\n';return 0;
}
可能的输出:
shuffled elements: 3 1 4 2 5
转自:http://www.cplusplus.com/reference/algorithm/shuffle/
这篇关于【C++】algorithm--shuffle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!