1

我不是 C++ 编码器,所以也许这很容易。

我有一个 Point 类的向量,我想找到 AABB 矩形:

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

我已经完成了一个 for 循环,保存了最小值和最大值(x 一次,y 一次),并使用一些 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)中的 'operator<<' 不匹配)this)->DrawerWidget::points.std::vector<_Tp, _Alloc>::begin with _Tp = ofVec3f, _Alloc = std::allocator, ((DrawerWidget*)this)->DrawerWidget::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 回答