Solved

find min/max values in stl container

Posted on 2001-09-11
4
747 Views
Last Modified: 2012-08-14
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
Comment
Question by:borlip
  • 2
4 Comments
 
LVL 4

Expert Comment

by:IainHere
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

by:Axter
ID: 6474480
50 points are assigned to an easy question.
0
 
LVL 1

Expert Comment

by:alex_r
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

by:
alex_r earned 7 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
Fast, free, private online code repository / editor 7 113
Problem with SqlConnection 4 184
boost::uuid crashes 17 35
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question