next_permutation,prev_permutation

1.next_permutation会取得[first,last)所标示的序列的下一个排列组合。如果没有下一个排列组合,便返回false;否则返回true。

其实现算法如下:

首先,从最尾端开始往前寻找两个相邻元素,令第一元素为*i,第二元素为*ii,且满足*i<*ii。

找到这样一组相邻元素后,再从尾端开始往前检验,找出第一个大于*i的元素,令其为*j,将i,j元素对调,再将ii之后的所有元素颠倒排列。

template<class BidirectionalIterator>
bool next_permutation(
      BidirectionalIterator first, 
      BidirectionalIterator last
)
{
    if(first == last)
        return false; //空序列

    BidirectionalIterator i = first;
    ++i;
    if(i == last)
        return false;  //一个元素,没有下一个序列了
    
    i = last;
    --i;

    for(;;) {
        BidirectionalIterator ii = i;
        --i;
        if(*i < *ii) {
            BidirectionalIterator j = lase;
            while(!(*i < *--j));

            iter_swap(i, j);
            reverse(ii, last);
            return true;
        }
        
        if(i == first) {
            reverse(first, last);  //全逆向,即为最小字典序列,如cba变为abc
            return false;
        }
    }

}

2.prev_permutation的功能与next_permutation相反,是找前一个排列组合。

算法如下:

首先,从最尾端开始往前寻找两个相邻元素,令第一元素为*i,第二元素为*ii,且满足*i>*ii。(与next不同,其为*i<*ii)

找到这样一组相邻元素后,再从尾端开始往前检验,找出第一个小于*i的元素,令其为*j,将i,j元素对调,再将ii之后的所有元素颠倒排列。(与next不同,其为大于*i)

template<class BidirectionalIterator>
bool next_permutation(
      BidirectionalIterator first, 
      BidirectionalIterator last
)
{
    if(first == last)
        return false; //空序列

    BidirectionalIterator i = first;
    ++i;
    if(i == last)
        return false;  //一个元素,没有下一个序列了
    
    i = last;
    --i;

    for(;;) {
        BidirectionalIterator ii = i;
        --i;
        if(*i > *ii) {  //不同
            BidirectionalIterator j = lase;
            while(!(*i > *--j));  //不同
 
            iter_swap(i, j);
            reverse(ii, last);
            return true;
        }
        
        if(i == first) {
            reverse(first, last);  //全逆向,即为最小字典序列,如cba变为abc
            return false;
        }
    }

}

例子:

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

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

  std::sort (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  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
3 2 1
After loop: 1 2 3

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort, std::reverse

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

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

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::prev_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

Output:

3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
After loop: 3 2 1


你可能感兴趣的:(next_permutation,prev_permutation)