Solved

find min/max values in stl container

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

  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 …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now