排列算法(reverse...rotate...next_permutation)

reverse

template <class BidirectionalIterator>
void reverse(BidirectionalIterator first,BidirectionalIterator last)//翻转range内iterator所指的元素而非iterator本身。

reverse_copy

  与reverse类似,只不过这个是把反转结果输出到result中,返回新产生的尾端,等同于先copy再reverse,[first,last)和[result,result+(last-first)不重叠

template <class BidirectionalIterator,class OutputIterator>
OutputIterator reverse_copy(BidirectionalIterator first,BidirectionalIterator last,OutputIterator result)

roate

  将range内的元素旋转,将middle处的元素旋转到first处,将middle+1处的元素旋转到first+1处,对于0<=n<=last-first区间内的每一个n,将元素*(first+n)赋值给*(first+(n+(last-middle))%(last-first)),也就是将[first,middle)与[middle,last)中的每个元素互换,可以用swap_ranges互换两个长度相同的range,用rotate换两个长度不同的相邻的range

inline void rotate(ForwardIterator first,ForwardIterator middle,ForwardIterator last)
{
    if(first==middle||middle==last)
        return ;
    __roate(first,middle,last,distance_type(first),iterator_categroy(first));
}

ForwardIterator版本

template <class ForwardIterator, class Distance>
void __rotate(ForwardIterator first, ForwardIterator middle,
    ForwardIterator last, Distance*, forward_iterator_tag) {
    for (ForwardIterator i = middle;;) {
        iter_swap(first, i);    // 前后段元素一一交换
        ++first;                // 双双前进1
        ++i;
        // 以下判断是前段[first, middle)先结束还是后段[middle,last)先结束
        if (first == middle) {        // 前段先结束
            if (i == last) return;     // 如果后段也结束了,整个就结束了
            middle = i;                // 否则进行调整,之后再进行迭代
        }
        else if (i == last)         // 后段先结束
            i = middle;            // 调整,之后再进行迭代
    }
}

Bidirectionalterator版本

// 分派函数(dispatch function)
template <class BidirectionalIterator>
inline void reverse(BidirectionalIterator first, BidirectionalIterator last) {
    __reverse(first, last, iterator_category(first));
}
// reverse 的 bidirectional iterator 版
template <class BidirectionalIterator>
void __reverse(BidirectionalIterator first, BidirectionalIterator last,
    bidirectional_iterator_tag) {
    while (true)
        if (first == last || first == --last)   //剩余需交换元素个数小于2
            return;
        else
            iter_swap(first++, last);             //iter_swap()函数为交换迭代器所指元素的值
}
// rotate 的 bidirectional iterator 版
template <class BidirectionalIterator, class Distance>
void __rotate(BidirectionalIterator first, BidirectionalIterator middle,
    BidirectionalIterator last, Distance*,
    bidirectional_iterator_tag) {
    reverse(first, middle);
    reverse(middle, last);
    reverse(first, last);
}

RandomIterator版本

  每一次__rotate_cycle只能将t/n的元素正确的左移,其中t为容器内元素个数,n为last-first和middle-first的最大公约数,而这些被移动的元素是以n为等间距的,所以循环n次,并分别以串的前n个元素为起点进行__rotate_cycle操作,就能保证将所有的元素都移动到正确的位置上。

// 最大公因数,利用辗转相除法
// __gcd() 应用于 __rotate() 的 random access iterator 版
template <class EuclideanRingElement>
EuclideanRingElement __gcd(EuclideanRingElement m, EuclideanRingElement n)
{
    while (n != 0) {          
        EuclideanRingElement t = m % n;
        m = n;
        n = t;
    }
    return m;
}
template <class RandomAccessIterator, class Distance, class T>
void __rotate_cycle(RandomAccessIterator first, RandomAccessIterator last,
    RandomAccessIterator initial, Distance shift, T*) {
    T value = *initial;                 //记下链首元素的值,接下来链首元素“出列”留下一个“槽”
    RandomAccessIterator ptr1 = initial;
    RandomAccessIterator ptr2 = ptr1 + shift;//指向链中下一元素
    while (ptr2 != initial) {
        *ptr1 = *ptr2;
        ptr1 = ptr2;                  //ptr1指向“槽”的位置
        if (last - ptr2 > shift)   //还没有到达最后一个元素
            ptr2 += shift;
        else
            ptr2 = first + (shift - (last - ptr2));
    }
    *ptr1 = value;
}
// rotate 的 random access iterator 版
template <class RandomAccessIterator, class Distance>
void __rotate(RandomAccessIterator first, RandomAccessIterator middle,
    RandomAccessIterator last, Distance*,
    random_access_iterator_tag) {
    // 以下迭代器的相减操作只适用于RandomAccessIterator
    // 取全长和前段长度的最大公因数
    Distance n = __gcd(last - first, middle - first);
    // 链数为gcd(m,n) 。链中元素个数为n/gcd(m,n)。
    while (n--)  //为了书写方便,先从最后一条链开始循环                                               
        __rotate_cycle(first, last, first + n, middle - first,
        value_type(first));
}

rotate_copy

  与rotate类似,但是他不会就地的旋转range,而是把旋转后的range输出到一result开头的range中,返回新产生的range的尾端

template <class ForwardIterator,class OutputIterator>
inline void rotate_copy(ForwardIterator first,ForwardIterator middle,ForwardIterator last,OutputIterator result);

next_permutation

  从尾端开始向前寻找两相邻元素,第一元素为i,第二元素为ii,而且*i<*ii,找到这样一组元素后,再从尾端开始向前检验,找出第一大于*i的元素令其为*j,将i和j对调,再将ii之后的所有元素颠倒即为所求。

tmplate <class BidirectionIterator>
bool next_permutation(BidirectionIterator first, BidirectionIterator last)
{
    if(first == last)
        return false;
    BidirectionIterator i = first;
    ++i;
    if(i == last)
        return false;
    i = last;
    --i;

    for(;;)
    {
        BidirectionIterator ii = i;
        --i;
        if(*i < *ii)
        {
            BidirectionIterator j = last;
            while(!(*i < *--j));
              iter_swap(i, j);
            reverse(ii, last);
            return true;
        }
        if(i == first) {
            reverse(first, last);
        return false;
        }
    }
}
//版本二:用自定义的function object
template <class BidirectionalIterator,class StrictweakOrdering>
bool next_permutation(BidirectionalIterator first,BidirectionalIterator last,StrictweakOrdering cmo);

prev_permutation

  从尾端开始向前寻找两相邻元素,第一元素为i,第二元素为ii,而且*i>*ii,找到这样一组元素后,再从尾端开始向前检验,找出第一小于*i的元素令其为*j,将i和j对调,再将ii之后的所有元素颠倒即为所求。

//版本一:operator<
template <class BidirectionalIterator>
bool prev_permutation(BidirectionalIterator first,BidirectionalIterator last)
{
    if(first == last) 
        return false;                                               
    BidirectionIterator i = first;                                                
    ++i;                                                                          
    if(i == last)
        return false;
    i = last;
    --i;

    for(;;) {
        BidirectionIterator ii = i;
        --i;                                                                      

        if(*ii < *i) {
            BidirectionIterator j = last;
            while(!(*--j < *i));
            iter_swap(i, j);
            reverse(ii, last);
            return true;
        }

        if(i == first) {
            reverse(first, last);
        return false;                                                         
        }                                                                         
    }                                            
}
//版本二:用自定义的function object
template <class BidirectionalIterator,class StrictweakOrdering>
bool prev_permutation(BidirectionalIterator first,BidirectionalIterator last,StrictweakOrdering cmo);

 

posted on 2018-06-07 16:59  tianzeng  阅读(268)  评论(0编辑  收藏  举报

导航