Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# find min/max values in stl container

Posted on 2001-09-11
Medium Priority
758 Views
Hi,
I have following data structure:
std::vector<Vertex3D*> ArrayVertex;
which is the vector of pointers.
Class Vertex3D contains some value of type double:
double curv;
what I need is to find min/max curv in my ArrayVertex fastest way using STL.
Thanks
0
Question by:borlip
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 4

Expert Comment

ID: 6474475
If vertex3D has comparison operators > and < that return based on curv, then you can simply use min_element and max_element.

minEl = min_element(ArrayVertex.begin(), ArrayVertex.end);

If it doesn't (probably the case) you'll have to define a function that does the comparison for you.  Something like

bool vertex_compare( const Vertex3D& a_, const Vertex3D& b_ )
{
if (a_.curv > b_.curv)
return true;

return false;
}

and then use it:

minEl = min_element(ArrayVertex.begin(), ArrayVertex.end, vertex_compare);
0

LVL 30

Expert Comment

ID: 6474480
50 points are assigned to an easy question.
0

LVL 1

Expert Comment

ID: 6474496
I would NOT recommend doing the following:
minEl = min_element(ArrayVertex.begin(), ArrayVertex.end);

min_element going to compare pointers of type Vertex3D, not the objects of type Vertex3D.

0

LVL 1

Accepted Solution

alex_r earned 28 total points
ID: 6474505
I think borlip has to use min_element version with 3 arguments and the vertext_compare should look like:

bool vertex_compare( const Vertex3D* a_, const Vertex3D* b_ )
...
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month7 days, 18 hours left to enroll