next_permutationとは?
概要
- 配列の要素を辞書順に並べたときに、現在の順列より次のものがあるかをbool型で返す関数。
第一引数 | 配列の先頭イテレータ |
---|---|
第二引数 | 配列の末尾イテレータ |
返り値 | bool型 |
詳細
- 引数となる配列は関数実行後に次の順列に並び替えられる。
- 次の順列がない(返り値がFalse)の場合は先頭に戻る。
使用例
配列{1, 2, 3}の並べ方を表示
ソースコード
#include <iostream> #include <vector> #include <algorithm> int main () { // 配列 std::vector<int> v = {1, 2, 3}; // next_permutationがfalseになるまで配列を表示 do { for (int i = 0; i < v.size(); i++) { std::cout << v[i] << " "; } std::cout << std::endl; } while (std::next_permutation(v.begin(), v.end())); // next_permutationがfalseとなった後のv std::cout << "next_permutationがfalseとなった後のv = "; for (int i = 0; i < v.size(); i++) { std::cout << v[i] << " "; } std::cout << std::endl; }
実行結果
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 next_permutationがfalseとなった後のv = 1 2 3