简单记录下我的学习过程 (代码为主)
//所有容器适用
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; }