STL 之reverse,reverse_copy,rotate,rotate_copy

作用:倒置或调换区间元素的位置

声明:

  1. #include   
  2. template <class biDirectionalItr>  
  3. void reverse(biDirectionalItr first, biDirectionalItr last);  
  4.   
  5. template <class biDirectionalItr, class outputItr>  
  6. outputItr reverse_copy(biDirectionalItr first, biDirectionalItr last,outputItr destFirst);  
  7.   
  8. template <class forwardItr>  
  9. void rotate(forwardItr first, forwardItr newFirst, forwardItr last);  
  10.   
  11. template <class forwardItr, class outputItr>  
  12. outputItr rotate_copy(forwardItr first, forwardItr middle, forwardItr middle,last,outputItr destFirst);  

示例代码:

  1. #include   
  2. #include   
  3.   
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  8. #include   
  9.   
  10. #include   
  11.   
  12. using namespace std;  
  13.   
  14. int main() {  
  15.     int temp[10] = {1,3,5,7,9,0,2,4,6,8};  
  16.     list<int> intList(temp,temp+10);  
  17.     list<int> resultList;  
  18.     list<int>::iterator listItr;  
  19.   
  20.     ostream_iterator<int> screen(cout," ");  
  21.     cout << "intList" << endl;  
  22.     copy(intList.begin(),intList.end(),screen);  
  23.     cout << endl;  
  24.     // 倒置  
  25.     reverse(intList.begin(),intList.end());  
  26.     cout << "intList" << endl;  
  27.     copy(intList.begin(),intList.end(),screen);  
  28.     cout << endl;  
  29.   
  30.     // reverse_copy  
  31.     reverse_copy(intList.begin(),intList.end(),back_inserter(resultList));  
  32.     cout << "resultList:" << endl;  
  33.     copy(resultList.begin(),resultList.end(),screen);  
  34.     cout << endl;  
  35.   
  36.     listItr = intList.begin();  
  37.     listItr++;  
  38.     listItr++;  
  39.     cout << "intList before ratete" << endl;  
  40.     copy(intList.begin(),intList.end(),screen);  
  41.     cout << endl;  
  42.     // rotate  
  43.     rotate(intList.begin(),listItr,intList.end());  
  44.     cout << "intList after ratete" << endl;  
  45.     copy(intList.begin(),intList.end(),screen);  
  46.     cout << endl;  
  47.   
  48.     resultList.clear();  
  49.     // rotate_copy  
  50.     rotate_copy(intList.begin(),listItr,intList.end(),back_inserter(resultList));  
  51.     cout << "intList after ratete_copy" << endl;  
  52.     copy(intList.begin(),intList.end(),screen);  
  53.     cout << endl;  
  54.   
  55.     cout << "resultList:" << endl;  
  56.     copy(resultList.begin(),resultList.end(),screen);  
  57.     cout << endl;  
  58.   
  59.     resultList.clear();  
  60.     // 牛逼闪闪的用法  
  61.     rotate_copy(intList.begin(),find(intList.begin(),intList.end(),6),intList.end(),back_inserter(resultList));  
  62.     cout << "resultList:" << endl;  
  63.     copy(resultList.begin(),resultList.end(),screen);  
  64.     cout << endl;  
  65.   
  66.     return 0;  
  67. }  

运行结果:

intList
1 3 5 7 9 0 2 4 6 8
intList
8 6 4 2 0 9 7 5 3 1
resultList:
1 3 5 7 9 0 2 4 6 8
intList before ratete
8 6 4 2 0 9 7 5 3 1
intList after ratete
4 2 0 9 7 5 3 1 8 6


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部