最小元素错误
我不是 C++ 编码员,所以也许这很容易。
我有一个 Point 类向量,我想找到 AABB 矩形:
- min x - min y
- min x - max y
- max x - min y
- max x - max 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;
}
但我收到了一个奇怪的错误,例如
错误:与“operator<<”不匹配在 'std::cout << std::min_element [与 _FIter = __gnu_cxx::__normal_iterator > >, _Compare = 布尔值 ()(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 放在 << 上的其他位置,则会出现类似的错误
i'm not a c++ coder so mayebe it's easy.
i have a vector of Point class and i want to find the AABB rectangle:
- min x - min y
- min x - max y
- max x - min y
- max x - max y
i've done a for loop saving the min and the max (once for x and once for y), and updating the value for each iteration with some ifs.
but i'm sure there is something smarter in std or in boost
.
for example i just tried:
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;
}
but i'm getting a strange error like
error: no match for ‘operator<<’ in ‘std::cout << std::min_element
[with _FIter = __gnu_cxx::__normal_iterator > >, _Compare = bool
()(const ofPoint&, const
ofPoint&)](((DrawerWidget)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)’
and a similar error if i put min_element somewhere else over the <<
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
min_element
返回一个指向最小元素的迭代器,您正尝试将其发送到cout
。使用:
并且您还需要重载
<<
,除非向量元素是cout
已经具有重载的<<
的类型代码> 运算符。min_element
returns an iterator to the minimum element, you are trying to send that tocout
.Use:
And you would also need to overload
<<
unless the vector element is an type for whichcout
already has an overloaded<<
operator.