Namespaces
Variants

std::experimental:: shuffle

From cppreference.net
定义于头文件 <experimental/algorithm>
template < class RandomIt >
void shuffle ( RandomIt first, RandomIt last ) ;
(库基础 TS v2)

对给定范围 [ first , last ) 内的元素进行重新排序,使得这些元素的每种可能排列都具有相等的出现概率,使用 每线程随机数引擎 作为随机数生成器。

目录

参数

first, last - 要随机重排的元素范围
-
RandomIt 必须满足 ValueSwappable LegacyRandomAccessIterator 的要求。

返回值

(无)

复杂度

first last 之间的距离呈线性关系。

示例

#include <experimental/algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string sample{"ABCDEF"};
    for (int i = 0; i != 4; ++i)
    {
        std::experimental::shuffle(sample.begin(), sample.end());
        std::cout << sample << '\n';
    }
}

可能的输出:

DACBFE
CDFBAE
BDCAFE
BAFCED

参见

(until C++17) (C++11)
随机重排范围内的元素
(函数模板)