我不是编码器,所以可能很容易 .
我有一个Point类的向量,我想找到AABB矩形:
-
min x - min y
-
min x - max y
-
max x - min y
-
max x - max y
我已经完成了一个for循环,保存了min和max(一次为x,一次为y),并用一些ifs更新每次迭代的值 .
但我确信在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中没有匹配'operator <<'[与_FIter = __gnu_cxx :: __ normal_iterator >>,_Compare = bool()(const ofPoint&,const ofPoint&)]((( DrawerWidget)this) - > DrawerWidget :: points.std :: vector <_Tp,_Alloc> ::以_Tp = ofVec3f开头,_Alloc = std :: allocator,((DrawerWidget *)this) - > DrawerWidget :: points.std :: vector <_Tp,_Alloc> :: end with _Tp = ofVec3f,_Alloc = std :: allocator,_compareX)'
如果我将min_element放在<<上的其他地方,则会出现类似的错误
1 回答
min_element
将迭代器返回到最小元素,您尝试将其发送到cout
.使用:
除非向量元素是
cout
已经有一个重载的<<
运算符的类型,否则你还需要重载<<
.