利用STL函数swap进行vector内的数据交换

来源:互联网 发布:常用设计模式 java 编辑:程序博客网 时间:2024/05/17 01:32
[cpp] view plaincopy
  1. #include   <iostream>  
  2. #include   <string>  
  3. #include   <algorithm>  
  4. #include   <vector>  
  5.   
  6. using   namespace   std;  
  7.   
  8. int   main(int   argc,   char*   argv[])  
  9. {  
  10.     vector <string>   vTest;  
  11.   
  12.     vTest.push_back( "a ");  
  13.     vTest.push_back( "b ");  
  14.     vTest.push_back( "c ");  
  15.     vTest.push_back( "d ");  
  16.   
  17.     swap(vTest[0],   vTest[3]);  
  18.   
  19.     vector <string> ::iterator   pos;  
  20.     for   (pos   =   vTest.begin();   pos   !=   vTest.end();   pos++)  
  21.     {  
  22.         cout   <<   *pos   <<   endl;  
  23.     }  
  24.   
  25.     return   0;  
  26. }  



原创粉丝点击