2

完全なコードはhttps://gist.github.com/1341623にあります

配列が他のベクトルのインデックスによって順序付けられるように、別のベクトルのインデックス配列(またはベクトル)を並べ替えたいと思います。ただし、vector::atのタイプは解決できません。

私は次のように試しました:

これで結構です

sort(v.begin(), v.end());

配列に従ってインデックスを並べ替えたいのですが、プレースホルダーはoperator[]をオーバーロードしません

sort(index,index+10, a[_1] < a[_2]);

ただし、operator+とoperator*をオーバーロードします

sort(index,index+10, *(a+_1) < *(a+_2));

ベクトルに従ってインデックスをソートしたいのですが、コンパイラは `vector ::at'のタイプを解決できません。

sort(index,index+10,
  bind(&(vector<int>::at), &v, _1) < bind(&(vector<int>::at), &v, _2));
// error: no matching function for call
// to ‘bind(<unresolved overloaded function type>, ...

Webで検索したところ、オーバーロードされたメソッドタイプを指定する必要があることがわかりましたが、コンパイラはそれでもタイプを解決できないと言っています。

sort(index,index+10,
   bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _1)
 < bind(&static_cast<const int (*)(size_t)>(vector<int>::at), &v, _2));
// error: invalid static_cast from type ‘&lt;unresolved overloaded function type>’
// to type ‘const int (*)(size_t)’ ...

vector :: atのバージョンを取得しようとしましたが、変換に失敗したようです。

vector<int>::const_reference (*vector_int_at)(vector<int>::size_type)(vector<int>::at);
sort(index,index+10,
  bind(&vector_int_at, &v, _1) < bind(&vector_int_at, &v, _2));
// error: no matches converting function ‘at’ to type ‘const int& (*)(size_t)’ ...

この問題に対して何ができますか?または私は何かを誤解していますか?

4

3 に答える 3

2

メンバー関数へのポインターとフリー関数へのポインターは異なるタイプであることに注意してください。試す: vector<int>::const_reference (vector<int>::*vector_int_at)(vector<int>::size_type) const = &vector<int>::at;

于 2011-11-05T15:43:13.317 に答える
1

私は通常、この種のものに関連するワームのさまざまな缶を回避するために転送関数を宣言します。

int vector_at(vector<int> const * v, size_t index) { return v->at(index); }

...

sort(index, index+10, bind(vector_at, &v, _1) < bind(vector_at, &v, _2));
于 2011-11-05T15:28:28.627 に答える
1

ラムダを使用しない理由は何ですか?

sort(index, index+10, [&a](int i, int j) { return a[i] < a[j]; });
于 2011-11-05T15:55:19.660 に答える