zoukankan      html  css  js  c++  java
  • C++ STL 逆转旋转 reverse reverse_copy rotate

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>

    using namespace std;

    int main()
    {
      vector<int> vec1;
      for (int k=0;k<10;k++)
      {
        vec1.push_back(k);
      }

      vector<int>::iterator vec_iter1;
      for (vec_iter1 = vec1.begin();vec_iter1 != vec1.end();++vec_iter1)
      {
        cout << *vec_iter1 << " ";
      }
      cout << endl;
      cout << "---------------------------------------" << endl;

      //逆转
      reverse(vec1.begin(), vec1.end());

      for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
      {
        cout << *vec_iter1 << " ";
      }
      cout << endl;
      cout << "---------------------------------------" << endl;

      vector<int> vec2;
      reverse_copy(vec1.begin(), vec1.end(), ostream_iterator<int>(cout," "));
      cout << endl;
      cout << "---------------------------------------" << endl;

      //旋转
      rotate(vec1.begin(), vec1.begin() + 5, vec1.end());

      for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
      {
        cout << *vec_iter1 << " ";
      }
      cout << endl;
      cout << "---------------------------------------" << endl;

      rotate(vec1.begin(), find(vec1.begin(), vec1.end(), 9), vec1.end());

      for (vec_iter1 = vec1.begin(); vec_iter1 != vec1.end(); ++vec_iter1)
      {
        cout << *vec_iter1 << " ";
      }
      cout << endl;
      cout << "---------------------------------------" << endl;


      system("pause");
      return 0;
    }

    =====================================================

    0 1 2 3 4 5 6 7 8 9
    ---------------------------------------
    9 8 7 6 5 4 3 2 1 0
    ---------------------------------------
    0 1 2 3 4 5 6 7 8 9
    ---------------------------------------
    4 3 2 1 0 9 8 7 6 5
    ---------------------------------------
    9 8 7 6 5 4 3 2 1 0
    ---------------------------------------
    请按任意键继续. . .

  • 相关阅读:
    还贷的那些事V——等增幅还贷的计算
    计算机中的颜色X——两颜色的偏转值
    高亮显示不区分大小写的关键字——ASP
    一道算法题,看看大家的思路
    一道算法题,看看大家的思路(续)
    IQCar的实现II——解题思路
    IQCar的实现I——IQCar的介绍
    Flex画流程图
    jQuery源码研究01
    javascript笔记:javascript里面不同function定义的区别
  • 原文地址:https://www.cnblogs.com/herd/p/11011743.html
Copyright © 2011-2022 走看看