我不是 C++ 编码器,所以也许这很容易。
我有一个 Point 类的向量,我想找到 AABB 矩形:
- 最小 x - 最小 y
- 最小 x - 最大 y
- 最大 x - 最小 y
- 最大 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 放在 <<