Namespaces
Variants

std::ranges:: shuffle

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
Constrained algorithms
All names in this menu belong to namespace std::ranges
Non-modifying sequence operations
Modifying sequence operations
Partitioning operations
Sorting operations
Binary search operations (on sorted ranges)
Set operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Permutation operations
Fold operations
Operations on uninitialized storage
Return types
定义于头文件 <algorithm>
调用签名
template < std:: random_access_iterator I, std:: sentinel_for < I > S, class Gen >

requires std:: permutable < I > &&
std:: uniform_random_bit_generator < std:: remove_reference_t < Gen >>

I shuffle ( I first, S last, Gen && gen ) ;
(1) (C++20 起)
(2) (C++20 起)
1) 对给定范围 [ first , last ) 内的元素进行重新排序,使得这些元素的每种可能排列都具有相等的出现概率。
2) (1) 相同,但使用 r 作为范围,如同以 ranges:: begin ( r ) 作为 first ,并以 ranges:: end ( r ) 作为 last

本页面描述的函数式实体是 算法函数对象 (非正式称为 niebloids ),即:

目录

参数

first, last - 定义待随机重排元素 范围 的迭代器-哨位对
r - 待随机重排的元素范围
gen - 随机数生成器

返回值

一个等于 last 的迭代器。

复杂度

恰好进行 ( last - first ) - 1 次交换。

可能的实现

struct shuffle_fn
{
    template<std::random_access_iterator I, std::sentinel_for<I> S, class Gen>
    requires std::permutable<I> &&
             std::uniform_random_bit_generator<std::remove_reference_t<Gen>>
    I operator()(I first, S last, Gen&& gen) const
    {
        using diff_t = std::iter_difference_t<I>;
        using distr_t = std::uniform_int_distribution<diff_t>;
        using param_t = typename distr_t::param_type;
        distr_t D;
        const auto n {last - first};
        for (diff_t i {1}; i < n; ++i)
            ranges::iter_swap(first + i, first + D(gen, param_t(0, i)));
        return ranges::next(first, last);
    }
    template<ranges::random_access_range R, class Gen>
    requires std::permutable<ranges::iterator_t<R>> &&
             std::uniform_random_bit_generator<std::remove_reference_t<Gen>>
    ranges::borrowed_iterator_t<R> operator()(R&& r, Gen&& gen) const
    {
        return (*this)(ranges::begin(r), ranges::end(r), std::forward<Gen>(gen));
    }
};
inline constexpr shuffle_fn shuffle {};

示例

#include <algorithm>
#include <array>
#include <iostream>
#include <random>
void print(const auto& a)
{
    for (const auto e : a)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    std::array a {'A', 'B', 'C', 'D', 'E', 'F'};
    print(a);
    std::random_device rd;
    std::mt19937 gen {rd()};
    for (int i {}; i != 3; ++i)
    {
        std::ranges::shuffle(a, gen);
        print(a);
    }
}

可能的输出:

A B C D E F
F E A C D B
E C B F A D
B A E C F D

参见

生成一个范围内元素的下一个较大字典序排列
(算法函数对象)
生成一个范围内元素的下一个较小字典序排列
(算法函数对象)
(until C++17) (C++11)
随机重排范围内的元素
(函数模板)