1 std::random_shuffle和std::shuffle
std::random_shuffle和std::shuffle处于头文件#include中。
std::random_shuffle和std::shuffle都用于对给定容器范围内的元素重新进行洗牌,打乱顺序重新排序。不过由于std::random_shuffle在迭代器版本(不指定随机函数的情况下)通常依赖std::srand,并且依赖于全局状态,这导致元素洗牌后的不会很理想,所以std::random_shuffle在C++14中已经被弃用,在C++17中被剔除。我们可以使用std::shuffle替代std::random_shuffle。
函数形式
template< class RandomIt >
void random_shuffle( RandomIt first, RandomIt last );
template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc& r );
template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r );
template< class RandomIt, class URBG >
void shuffle( RandomIt first, RandomIt last, URBG&& g );
C++
Copy
参数含义
- first : 随机洗牌元素范围第一个元素
- lats:随机洗牌元素范围最后一个元素
- r:返回一个随机选择的可转换类型的值的函数对象
- g:结果类型可转换为UniformRandomBitGenerator的参数
#include
#include
#include
#include
template
void PrintVector(const std::vector& vector)
{
for_each(vector.begin(), vector.end(), [](T x) {
std::cout
关注
打赏
