zoukankan      html  css  js  c++  java
  • STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中。。

     

    简单记录下我的学习过程 (代码为主)


    //全部容器适用


    reverse(b,e)        //逆转区间数据


    reverse_copy(b,e,b2)


    /**------http://blog.csdn.net/u010579068------**/
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<vector>
    #include<list>
    #include<deque>
    #include<algorithm>
    using namespace std;
    
    /*****************************************
    //全部容器适用
    reverse(b,e)        //逆转区间数据
    reverse_copy(b,e,b2)
    *****************************************/
    /**----------------------------------------------------------------------------------
    STL算法 - 变序性算法
    
    reverse()           //逆转
    reverse_copy()
    rotate()            //旋转
    rotate_copy()
    next_permutation()
    prev_permutation()
    random_shuffle()
    partition()
    stable_partition()
    ----------------------------------------------------------------------------------**/
    /*************************************************************************************
    std::reverse                     全部排序容器适用                           algorithm
    --------------------------------------------------------------------------------------
    <algorithm>template <class BidirectionalIterator>
      void reverse ( BidirectionalIterator first, BidirectionalIterator last);
    
    //eg:
    template <class BidirectionalIterator>
      void reverse ( BidirectionalIterator first, BidirectionalIterator last)
    {
      while ((first!=last)&&(first!=--last))
        swap (*first++,*last);
    }
    *************************************************************************************/
    
    /*************************************************************************************
    std::reverse_copy                   全部排序容器适用                        algorithm
    --------------------------------------------------------------------------------------
    template <class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy ( BidirectionalIterator first,
                                    BidirectionalIterator last, OutputIterator result );
    //eg:
    template <class BidirectionalIterator, class OutputIterator>
      OutputIterator reverse_copy ( BidirectionalIterator first,
                                    BidirectionalIterator last, OutputIterator result )
    {
      while (first!=last) *result++ = *--last;
      return 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
    
        reverse(myvector.begin(),myvector.end());       // 9 8 7 6 5 4 3 2 1
    
        // print out content:
        cout << "myvector contains:";
        for (it=myvector.begin(); it!=myvector.end(); ++it)
            cout << " " << *it;
        cout << endl;
    
    
         int myints[] = {1,2,3};
    
      cout << "The 3! possible permutations with 3 elements:
    ";
    
      sort (myints,myints+3);
      reverse (myints,myints+3);
    
      do {
        cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
      } while ( prev_permutation (myints,myints+3) );
    
        /**-------------------------------------------------------------------------------------**/
    //    int myints[] = {1,2,3,4,5,6,7,8,9};
        deque<int> mydeque;
        deque<int>::iterator iq;
    
        mydeque.resize(9);
    
        reverse_copy (myvector.begin(), myvector.end(), mydeque.begin());
    
        // print out content:
        cout << "mydeque  contains:";
        for (iq=mydeque.begin(); iq!=mydeque.end(); ++iq)
            cout << " " << *iq;
        cout << endl;
    
    
    
        return 0;
    }
    


  • 相关阅读:
    第三节 单因素方差分析
    第四十一节 ORM介绍和用元类实现
    第四十节 通过type创建复杂的类,元类应用
    第二节 检验方法使用条件考察
    HDFS HA误删namenode后报错Nameservice testCluster has no SecondaryNameNode or High-Availability partner的恢复
    spark sql cache时发现的空字符串问题
    centos7环境下ELK部署之elasticsearch
    CDH升级 5.7.5 --> 5.13.3(tar包方式)
    CDH部署(以5.7.5为例)
    人生苦短,Let's Go
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6962109.html
Copyright © 2011-2022 走看看