max_element man page on OpenIndiana

Man page or keyword search:  
man Server   20441 pages
apropos Keyword Search (all sections)
Output format
OpenIndiana logo
[printable version]

max_element(3C++)		       -		     max_element(3C++)

Standard C++ Library Copyright 1998, Rogue Wave Software, Inc.

NAME
       max_element

	- Finds the maximum value in a range.

SYNOPSIS
       #include <algorithm>
       template <class ForwardIterator>
       ForwardIterator
 max_element(ForwardIterator first, ForwardIterator last);

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

DESCRIPTION
       The  max_element algorithm returns an iterator that denotes the maximum
       element in a sequence. If the sequence contains more than one  copy  of
       the  element, the iterator points to its first occurrence. The optional
       argument comp defines a comparison function that can be used  in	 place
       of the default operator<.

       Algorithm max_element returns the first iterator i in the range [first,
       last) such that for any iterator j in the same range the following cor‐
       responding conditions hold:

       !(*i < *j)

       or

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

COMPLEXITY
       Exactly	max((last  -  first) - 1, 0) applications of the corresponding
       comparisons are done for max_element.

EXAMPLE
//
// max_elem.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iostream>
using namespace std;

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,
		     less<int>());
   // 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(),
		     less<int>());
   // it4 = v1.begin() + 1

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

  return 0;
 }

Program Output

64 32 1 3

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

       vector<int,allocator<int> >

       instead of:

       vector<int>

       If your compiler does not support namespaces, then you do not need  the
       using declaration for std.

SEE ALSO
       max, min, min_element

Rogue Wave Software		  02 Apr 1998		     max_element(3C++)
[top]

List of man pages available for OpenIndiana

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net