min_element


Return the minimum item within a range.

Return an iterator positioned at the minimum element in the range [ first, last ). The first version uses operator< to perform the comparisons, whereas the second version uses the binary function compare .

Library

Standards<ToolKit>

Declaration


#include <algorithm>

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

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

Complexity

Time complexity is linear, as ( last - first ) comparisons are performed. Space complexity is constant.

Example <ospace/osstd/examples/minelem1.cpp>
#include <iostream>
#include <algorithm>

int numbers[ 6 ] = { -10, 15, -100, 36, -242, 42 };

void
main()
  {
  cout << *min_element( numbers, numbers + 6 ) << "\n";
  }

-242
Example <ospace/osstd/examples/minelem2.cpp>
#include <iostream>
#include <string.h>
#include <algorithm>

bool
str_compare( const char* a_, const char* b_ )
  {
  return ::strcmp( a_, b_ ) < 0 ? 1 : 0;
  }

char* names[] = { "Brett", "Graham", "Jack", "Mike", "Todd" };

void
main()
  {
  const unsigned names_ct = sizeof( names )/sizeof( names[ 0 ] );
  cout << *min_element( names, names + names_ct, str_compare ) << "\n";
  }

Brett

Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.