STL_算法_旋转(rotaterotate_copy)

Posted 寻找星空的孩子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL_算法_旋转(rotaterotate_copy)相关的知识,希望对你有一定的参考价值。

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)


//所有容器适用


rotate(b,m,e)       //以m-b为单位,向前移动(旋转)


rotate_copy(b,m,e,b2)


/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;

/*****************************************
//所有容器适用
rotate(b,m,e)       //以m-b为单位,向前移动(旋转)
rotate_copy(b,m,e,b2)
*****************************************/
/**----------------------------------------------------------------------------------
STL算法 - 变序性算法

reverse()           //逆转
reverse_copy()
rotate()            //旋转
rotate_copy()
next_permutation()
prev_permutation()
random_shuffle()
partition()
stable_partition()
----------------------------------------------------------------------------------**/
/*************************************************************************************
std::rotate                     所有排序容器适用                           algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator>
  void rotate ( ForwardIterator first, ForwardIterator middle,
                ForwardIterator last );
//eg:
template <class ForwardIterator>
  void rotate ( ForwardIterator first, ForwardIterator middle,
                ForwardIterator last )

  ForwardIterator next = middle;
  while (first!=next)
  
    swap (*first++,*next++);
    if (next==last) next=middle;
    else if (first == middle) middle=next;
  

*************************************************************************************/

/*************************************************************************************
std::rotate_copy                   所有排序容器适用                         algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,
                               ForwardIterator last, OutputIterator result );
//eg:
template <class ForwardIterator, class OutputIterator>
  OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,
                               ForwardIterator last, OutputIterator result )

  result=copy (middle,last,result);
  return copy (first,middle,result);

*************************************************************************************/


int main()

    vector<int> myvector;
    vector<int>::iterator it;

    // set some values:
    for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

    rotate(myvector.begin(),myvector.begin()+4,myvector.end());
    //5 6 7 8 9 1 2 3 4

    // print out content:
    cout << "myvector contains:";
    for (it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    cout << endl;

    /**----------------------------------------------------------------------------**/
    int myints[] = 10,20,30,40,50,60,70,80,90;

    myvector.clear();
    myvector.resize(9);
    list<int> li(9);
    list<int>::iterator iter;

    copy(myints,myints+9,myvector.begin());

    rotate_copy(myvector.begin(),myvector.begin()+4,myvector.end(),li.begin());

    // print out content:
    cout << "mylist contains:";
    for (iter=li.begin(); iter!=li.end(); ++iter)
        cout << " " << *iter;
    cout << endl;


    return 0;



以上是关于STL_算法_旋转(rotaterotate_copy)的主要内容,如果未能解决你的问题,请参考以下文章

STL_算法_Heap算法(堆排)(精)

STL_算法_06_遍历算法

STL_算法_Heap算法(堆排)(精)

STL_算法_Heap算法(堆排)(精)

STL_算法_查找算法(findfind_if)

STL_算法_局部排序(partial_sortpartial_sort_copy)