Solved

find min/max values in stl container

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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