2
我試圖打印字符串向量的所有排列。此代碼按預期工作:字符串向量的next_permutation正在跳過一個排列
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
vector<string> v;
v.push_back("+1");
v.push_back("x4");
do {
cout << v[0] << " " << v[1] << endl;
} while (next_permutation(v.begin(), v.end()));
}
輸出:
+1 x4
x4 +1
但是當我改變 「X4」 爲 「* 4」 的next_pemutation循環迭代一次。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
vector<string> v;
v.push_back("+1");
v.push_back("*4");
do {
cout << v[0] << " " << v[1] << endl;
} while (next_permutation(v.begin(), v.end()));
}
輸出:
+1 *4
其他字符,如#似乎有同樣的效果。爲什麼會發生?
它需要開始排序要經過所有的人。 – chris
「下一步」是指「下一個所有排列的特定順序」。 –
事實上,請首先向我們解釋你不明白的[文檔](http://en.cppreference.com/w/cpp/algorithm/next_permutation)部分,因此我們可以給出一個有用的答案。 –