STL_알고리즘회전 (rotate, rotate copy)

C + + Primer 학습 중...  나의 학습 과정 을 간단하게 기록 하 다. (코드 위주) / / 모든 용기 에 rotate (b, m, e) 적용      //m - b 단위 로 앞으로 이동 (회전) rotatecopy(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;
}

좋은 웹페이지 즐겨찾기