Solved

find min/max values in stl container

Posted on 2001-09-11
4
746 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

856 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