2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

順列列挙

Last updated at Posted at 2020-01-05

整数型配列やVectorを順列で一つ一つ処理をしていく方法
next_permutation関数を使う。
関数の使い方が少し独特である。
全ての順列を取得したい場合は、この関数に最初に与える範囲が、
昇順にソート済みになっていること。
つまりこの関数は、現在の状態から順々に並び替えて次の値を取得する、ということ

順列は自分で実装する場合は再帰を使うが、わりと手間である。

整数型配列

	int a[5] = { 1,2,3,4,5 };
	do {
		for(int i = 0;i<4;i++){
			cout << a[i] << " ";
		}
		cout << endl;
	} while (next_permutation(a, a + 4));

Vector

	vector<int> v = { 6,7,8,9 };
	do {
		for(auto V : v){
			cout << V << " ";
		}
		cout << endl;
	} while (next_permutation(v.begin(), v.end()));

文字列


	string b = "abcde";
	do {
		cout << b << endl;
	} while (next_permutation(b.begin(), b.end()));
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?