Skip to content. Skip to navigation

ICTP Portal

You are here: Home Manuals on-line PGI Compiler pgC_lib min_element
Personal tools
Document Actions


Click on the banner to return to the class reference home page.




Finds the minimum value in a range.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)



#include <algorithm>

template <class ForwardIterator>
 min_element(ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class Compare>
 min_element(ForwardIterator first, ForwardIterator last,
             Compare comp);


The min_element algorithm returns an iterator that denotes the minimum element in a sequence. If the sequence contains more than one copy of the minimum element, the iterator points to the first occurrence of the element. In the second version of the function, the optional argument comp defines a comparison function that can be used in place of the default operator<. This function can be used with all the datatypes provided by the standard library.

Algorithm min_element returns the first iterator i in the range [first, last) such that for any iterator j in the range same range, the following corresponding conditions hold:

 !(*j < *i)


comp(*j, *i) == false. 


min_element performs exactly max((last - first) - 1, 0) applications of the corresponding comparisons.


// max_elem.cpp
 #include <algorithm>
 #include <vector>
 #include <iostream.h>

 int main(void)
   typedef vector<int>::iterator iterator; 
   int d1[5] = {1,3,5,32,64}; 
   // set up vector 
   vector<int>      v1(d1,d1 + 5); 

   // find the largest element in the vector
   iterator it1 = max_element(v1.begin(), v1.end());
   // it1 = v1.begin() + 4
   // find the largest element in the range from
   // the beginning of the vector to the 2nd to last
   iterator it2 = max_element(v1.begin(), v1.end()-1, 
   // it2 = v1.begin() + 3
   // find the smallest element 
   iterator it3 = min_element(v1.begin(), v1.end());  
   // it3 = v1.begin() 
   // find the smallest value in the range from
   // the beginning of the vector plus 1 to the end
   iterator it4 = min_element(v1.begin()+1, v1.end(), 
   // it4 = v1.begin() + 1

   cout << *it1 << " " << *it2 << " " 
        << *it3 << " " << *it4 << endl;
   return 0;

Output :
64 32 1 3


If your compiler does not support default template parameters then you need to always supply the Allocator template argument. For instance you'll have to write:

vector<int,allocator<int> >

instead of:


See Also

max, max_element, min

©Copyright 1996, Rogue Wave Software, Inc.

Powered by Plone This site conforms to the following standards: