std::experimental::shuffle
来自 cppreference.cn
< cpp | experimental
定义于头文件 <experimental/algorithm> |
||
template< class RandomIt > void shuffle( RandomIt first, RandomIt last ); |
(库基础 TS v2) | |
重排给定范围 [
first,
last)
中的元素,使得这些元素的每种可能的排列都具有相等的出现概率,使用每线程随机数引擎作为随机数生成器。
目录 |
[edit] 参数
first, last | - | 要随机打乱的元素范围 |
-RandomIt 必须满足 ValueSwappable 和 LegacyRandomAccessIterator 的要求。 |
[edit] 返回值
(无)
[edit] 复杂度
与 first 和 last 之间的距离呈线性关系。
[edit] 示例
运行此代码
#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
[edit] 参见
(直到 C++17)(C++11 起) |
随机重排范围中的元素 (函数模板) |