STL中vector容器实现反转(reverse)


vector容器中实现可以通过以下两种方式实现:

#include "stdafx.h"
#include <vector>
#include <iostream>
//#include <math.h>

#include <algorithm>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

    vector<int> arrayInt;
    arrayInt.resize(10);
    for (int i=0;i<10;i++)
    {
        arrayInt[i]=i;
    }

    
    vector<int> arrayRever;
    arrayRever.reserve(arrayInt.size());

    //vector反转

    //------------------------------------------------------------------------------
    //>>>
    //方法一:使用vector自带的反转迭代器reverse_iterator,rbegin(),rend()
    vector<int>::reverse_iterator riter;
    for (riter=arrayInt.rbegin();riter!=arrayInt.rend();riter++)
    {
        arrayRever.push_back(*riter);
    }
    //<<<
    //------------------------------------------------------------------------------


    //------------------------------------------------------------------------------
    //>>>
    //方法二:使用<algorthm>中的reverse()
    //arrayRever=arrayInt;
    //reverse(arrayRever.begin(),arrayRever.end());
    //<<<
    //------------------------------------------------------------------------------

    //
    for (int i=0;i<arrayRever.size();i++)
    {
        cout<<"arrayRever["<<i<<"]"<<"    "<<arrayRever[i]<<endl;
    }

    return 0;
}

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM