min_element error

1.1k views Asked by At

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:

  1. min x - min y
  2. min x - max y
  3. max x - min y
  4. 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 <<

1

There are 1 answers

1
Alok Save On BEST ANSWER

min_element returns an iterator to the minimum element, you are trying to send that to cout.

Use:

std::cout<<*min_element()

And you would also need to overload << unless the vector element is an type for which cout already has an overloaded << operator.