• 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
    ---------------------------------------
    请按任意键继续. . .

  • 相关阅读:
    财务统计
    Drupal 7 模 .info 文件描述
    怎么样MyEclipse配置Tomcat?
    QQ旅程server分析01-网关server
    UVa 988
    人工智能的策略,如果国家优先发展”梦想成真”?
    苹果公司的新的编程语言 Swift 高级语言(十一)--初始化类的析构函数的一个实例
    【剑指offer】两个队列实现堆栈
    证券从业改革
    Qt调用word 例子
  • 原文地址:https://www.cnblogs.com/herd/p/11011743.html
Copyright © 2020-2023  润新知