把编译器的选择项设置为最严格状态。
1 #include <iostream> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 6 7 8 using namespace std; 9 10 //利用类模板生成实例 11 typedef vector < int > IntArray; 12 13 //显示数组 14 void put_array(int x[],int size) { 15 for(int i=0;i<size;i++) 16 cout<<x[i]<<" "; 17 cout<<endl; 18 } 19 20 //显示vector容器中的元素 21 void put_vector(IntArray v) 22 { 23 IntArray::iterator theIterator; 24 25 for (theIterator=v.begin();theIterator!=v.end();++theIterator){ 26 cout<<(*theIterator)<<" "; 27 } 28 cout<<endl; 29 } 30 31 //在main()函数中测试reverse()和reverse_copy()算法 32 int main(int argc, char** argv) { 33 34 //-------------------------------------------- 35 // reverse()和reverse_copy()算法对普通数组处理 36 //--------------------------------------------- 37 int x[]={1,3,5,7,9}; 38 cout<<"x[]:"; 39 put_array(x,5); 40 41 //reverse()反转x数组并显示 42 reverse(x,x+5); 43 cout<<"x[]:"; 44 put_array(x,5); 45 46 int y[]={2,4,6,8,10}; 47 cout<<"y[]:"; 48 put_array(y,5); 49 50 //reverse_copy()反转y数组的部分元素并拷贝到x数组第2个元素位置 51 reverse_copy(y+1,y+3,x+1); 52 cout<<"x[]:"; 53 put_array(x,5); 54 cout<<"y[]:"; 55 put_array(y,5); 56 //-------------------------------------------- 57 // reverse()和reverse_copy()算法对vector容器的处理 58 //--------------------------------------------- 59 //声明intvector容器和迭代器ii 60 IntArray intvector; 61 62 //向intvector容器中插入元素 63 for (int i=1; i<=10; i++) { 64 intvector.push_back(i); 65 }; 66 67 //显示intvector容器中的元素值 68 cout << "intvector: "<<endl; 69 put_vector(intvector); 70 71 //reverse()对于vector容器的处理 72 reverse(intvector.begin(),intvector.end()); 73 cout << "intvector: "<<endl; 74 put_vector(intvector); 75 76 // reverse_copy对于vector容器的处理 77 IntArray temp(5); 78 reverse_copy(intvector.begin()+2,intvector.begin()+7,temp.begin()); 79 cout << "temp: "<<endl; 80 put_vector(temp); 81 82 return 0; 83 }
原文地址:https://www.cnblogs.com/borter/p/9418312.html
时间: 2024-10-04 00:34:59