Min/max:

std::min

<algorithm>
template <class T> const T& min ( const T& a, const T& b );
template <class T, class Compare>
  const T& min ( const T& a, const T& b, Compare comp );
Return the lesser of two arguments
Returns the lesser of  a  and  b . If both are equivalent,  a  is returned.

The comparison uses  operator<  for the first version, and  comp  for the second.

The behavior of this function template is equivalent to:
1
2
3
template <class T> const T& min ( const T& a, const T& b ) {
  return !(b<a)?a:b;     // or: return !comp(b,a)?a:b; for the comp version
}

std::max

<algorithm>
template <class T> const T& max ( const T& a, const T& b );
template <class T, class Compare>
  const T& max ( const T& a, const T& b, Compare comp );
Return the greater of two arguments
Returns the greater of  a  and  b . If both are equivalent,  a  is returned.

The comparison uses  operator<  for the first version, and  comp  for the second.

The behavior of this function template is equivalent to:
1
2
3
template <class T> const T& max ( const T& a, const T& b ) {
  return (a<b)?b:a;     // or: return comp(a,b)?b:a; for the comp version
}

std::min_element

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

template <class ForwardIterator, class Compare>
  ForwardIterator min_element ( ForwardIterator first, ForwardIterator last,
                                Compare comp );
Return smallest element in range
Returns an iterator pointing to the element with the smallest value in the range  [first,last) . The comparisons are performed using either  operator<  for the first version, or  comp  for the second; An element is the smallest if no other element compares less than it (it may compare equal, though).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
template <class ForwardIterator>
  ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator lowest = first;
  if (first==last) return last;
  while (++first!=last)
    if (*first<*lowest)    // or: if (comp(*first,*lowest)) for the comp version
      lowest=first;
  return lowest;
}

std::max_element

<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 );
Return largest element in range
Returns an iterator pointing to the element with the largest value in the range  [first,last) . The comparisons are performed using either  operator<  for the first version, or  comp  for the second; An element is the largest if it does not compare less than any other element (it may compare equal, though).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
template <class ForwardIterator>
  ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
  ForwardIterator largest = first;
  if (first==last) return last;
  while (++first!=last)
    if (*largest<*first)    // or: if (comp(*largest,*first)) for the comp version
      largest=first;
  return largest;
}

std::lexicographical_compare

<algorithm>
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2 );

template <class InputIterator1, class InputIterator2, class Compare>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2,
                                 Compare comp );
Lexicographical less-than comparison
Returns  true  if range  [first1,last1)  compares lexicographically less than the range  [first2,last2) .

Lexicographical comparison is used for example to sort words alphabetically in dictionaries (or in the list of algorithms right here on the left); It involves comparing sequentially the elements that have the same position in both ranges against each other until one element is not equivalent to the other. The result of comparing these first non-matching elements is the result of the lexicographical comparison.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2 )
{
  while (first1!=last1)
  {
    if (first2==last2 || *first2<*first1) return false;
    else if (*first1<*first2) return true;
    first1++; first2++;
  }
  return (first2!=last2);
}

std::next_permutation

<algorithm>
template <class BidirectionalIterator>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last );

template <class BidirectionalIterator, class Compare>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last, Compare comp);
Transform range to next permutation
Rearranges the elements in the range [first, last) into the lexicographically next greater permutation of elements. The comparisons of individual elements are performed using either  operator<  for the first version, or  comp  for the second.

A permutation is each one of the  N!  possible arrangements the elements can take (where  N  is the number of elements in the range). Different permutations can be ordered according on how they compare lexicographicaly to each other; The first such-sorted possible permutation (the one that would compare lexicographically smaller to all other permutations) is the one which has all its elements sorted in ascending order, and the largest has all its elements sorted in descending order.

If the function can determine the next higher permutation, it rearranges the elements as such and returns  true . If that was not possible (because it is already at the largest), it rearranges the elements according to the first permutation (sorted in ascending order) and returns  false .

Parameters

first, last
Bidirectional iterators to the initial and final positions of the sequence. The range used is  [first,last), which contains all the elements between  first and  last, including the element pointed by  first but not the element pointed by  last.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns  true if the first argument is to be considered less than the second argument.

Return value

true  if the function could rearrange the object as a lexicographicaly greater permutation. Otherwise, the function returns  false  to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// next_permutation
#include <iostream>
#include <algorithm>
using namespace std;

int main () {
  int myints[] = {1,2,3};

  cout << "The 3! possible permutations with 3 elements:\n";

  sort (myints,myints+3);

  do {
    cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
  } while ( next_permutation (myints,myints+3) );

  return 0;
}


Output:
The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2

std::prev_permutation

<algorithm>
template <class BidirectionalIterator>
  bool prev_permutation (BidirectionalIterator first,
                         BidirectionalIterator last );

template <class BidirectionalIterator, class Compare>
  bool prev_permutation (BidirectionalIterator first,
                         BidirectionalIterator last, Compare comp);
Transform range to previous permutation
Rearranges the elements in the range [first, last) into the lexicographically next smaller permutation of elements. The comparisons of individual elements are performed using either  operator<  for the first version, or  comp  for the second.

A permutation is each one of the  N!  possible arrangements the elements can take (where  N  is the number of elements in the range). Different permutations can be ordered according on how they compare lexicographicaly to each other; The first such-sorted possible permutation (the one that would compare lexicographically smaller to all other permutations) is the one which has all its elements sorted in ascending order, and the largest has all its elements sorted in descending order.

If the function can determine the previous smaller permutation, it rearranges the elements as such and returns true . If that was not possible (because it is already at the smallest), it rearranges the elements according to the last permutation (sorted in descending order) and returns  false .

Parameters

first, last
Bidirectional iterators to the initial and final positions of the sequence. The range used is  [first,last), which contains all the elements between  first and  last, including the element pointed by  first but not the element pointed by  last.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns  true if the first argument is to be considered less than the second argument.

Return value

true  if the function could rearrange the object as a lexicographicaly smaller permutation. Otherwise, the function returns  false  to indicate that the arrangement is not smaller than the previous, but the largest possible (sorted in descending order).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// prev_permutation
#include <iostream>
#include <algorithm>
using namespace std;

int main () {
  int myints[] = {1,2,3};

  cout << "The 3! possible permutations with 3 elements:\n";

  sort (myints,myints+3);
  reverse (myints,myints+3);

  do {
    cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
  } while ( prev_permutation (myints,myints+3) );

  return 0;
}


Output:
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值