名前空間
変種
操作

std::experimental::shuffle

From cppreference.com
 
 
 
 
ヘッダ <experimental/algorithm> で定義
template< class RandomIt >
void shuffle( RandomIt first, RandomIt last );
(Library Fundamentals TS v2)

指定された範囲[firstlast)の要素を並べ替えます。これにより、それらの要素の可能なすべての順列が、スレッドごとの乱数エンジンを乱数ジェネレーターとして使用して、等しい確率で出現します。

目次

[編集] パラメータ

first, last - ランダムにシャッフルする要素の範囲
-
RandomItは、ValueSwappableおよびLegacyRandomAccessIteratorの要件を満たす必要があります。

[編集] 戻り値

(なし)

[編集] 複雑さ

firstlast の距離に対して線形。

[編集]

#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

[編集] 関連項目

(C++17まで)(C++11)
範囲内の要素をランダムに並べ替える
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)