特定のベクトルに対して可能なすべてのベクトル回転の組み合わせを検索したい。私のコードは、特定の要素をベクトル内で逐次見つけ、その周りを回転しますが、この論理は{1,1,2}のように連続して発生すると失敗します 以下はコードスニペットです。問題は、できれば、私のforループの中にif elseループと言うことができます。C++ベクトル回転すべての組み合わせ
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
vector<vector<int> > allrot(const vector<int>& a);
int main()
{
int myints[] = { 1, 1, 2 };
std::vector<int> a (myints, myints + sizeof(myints)/sizeof(int));
std::vector<vector<int> > b;
b = allrot(a);
}
vector<vector<int> > allrot(const vector<int>& a) {
std::vector<vector<int> > b;
for(int i = 1; i <= a.size(); i++) {
//int k;
//if (a[i] == a[i+1])
//k = a [i+1];
//else
//k = a[i];
auto pivot = std::find(a.begin(), a.end(), a[i]);
std::vector<int> dest(a.size());
std::rotate_copy(a.begin(), pivot, a.end(), dest.begin());
for (const auto &i : dest) {
std::cout << i << ' ';
}
std::cout << '\n';
b.push_back(dest);
}
return b;
}
質問が素朴に見える場合は、私はC++を新しくしています。
これは宿題ではなく、プログラムに必要な場合は、[std :: next_permutation](http://en.cppreference。com/w/cpp/algorithm/next_permutation) –
C++では、配列(およびstd :: vector)のインデックスは0ベースです。次のように 'auto pivot = std :: find(a.begin()、a.end()、** a [i] **); ' –
@AdrianColomitchi、私はすべての順列のように感じ、すべての回転は非常に異なっています。 (すなわち、「n回の回転があるが、「n!」の順列がある)。 –