vector容器中实现反转可以通过以下两种方式实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#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; } |
转自:https://www.cnblogs.com/vranger/p/3502885.html