1

私はC++コーダーではないので、簡単かもしれません。

Pointクラスのベクトルがあり、AABB長方形を見つけたい:

  1. min x-min y
  2. 最小x-最大y
  3. 最大x-最小y
  4. max x-max y

最小値と最大値を保存するforループ(xに1回、yに1回)を実行し、各反復の値をいくつかのifで更新しました。

しかし、私はstdまたはboostにもっと賢いものがあると確信しています。

たとえば、私はちょうど試しました:

vector<ofPoint> points;
// ....

bool _compareX(ofPoint const &p1, ofPoint const &p2) { return p1.x > p2.x; }
bool _compareY(ofPoint const &p1, ofPoint const &p2) { return p1.y > p2.y;}

void DrawerWidget::foo()
{
    cout << std::min_element(points.begin(), points.end(), &_compareX) << endl;
}

しかし、私は次のような奇妙なエラーが発生しています

エラー:'std :: cout << std :: min_element [with _FIter = __gnu_cxx :: __ normal_iterator >>、_Compare = bool()(const ofPoint&、const ofPoint&)](((DrawerWidget)this)-> DrawingWidget :: points.std :: vector <_Tp、_Alloc> :: begin with _Tp = ofVec3f、_Alloc = std :: allocator、((DrawerWidget *)this)-> DrawingWidget :: points.std: :vector <_Tp、_Alloc> :: end with _Tp = ofVec3f、_Alloc = std :: allocator、_compareX)'</ p>

min_elementを<<のどこかに置いた場合も同様のエラーが発生します

4

1 に答える 1

6

min_elementイテレータを最小要素に返します。これをに送信しようとしていますcout

使用する:

std::cout<<*min_element()

<<また、ベクトル要素がcoutすでにオーバーロードされた演算子を持っているタイプでない限り、オーバーロードする必要があります<<

于 2012-01-30T18:42:24.677 に答える