【问题标题】:min_element errormin_element 错误
【发布时间】:2012-02-22 13:10:31
【问题描述】:

我不是 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 >, _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 放在 上的其他位置,则会出现类似的错误

【问题讨论】:

    标签: c++ std stdvector min aabb


    【解决方案1】:

    min_element 返回一个指向最小元素的迭代器,您正试图将它发送给cout

    用途:

    std::cout<<*min_element()
    

    您还需要重载&lt;&lt;,除非向量元素是cout 已经有一个重载的&lt;&lt; 运算符的类型。

    【讨论】:

    • @nkint:这与主要问题中提出的问题没有完全不同。请原谅我可能误解了,但我不确定你到底想做什么。
    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 2013-10-26
    • 2016-04-06
    • 2018-03-22
    • 2012-02-21
    相关资源
    最近更新 更多