vector中erase用法注意事项

vector::erase():从指定容器删除指定位置的元素或某段范围内的元素
vector::erase()方法有两种重载形式
如下:
iterator erase(   iterator _Where);
iterator erase(   iterator _First,   iterator _Last);
如果是删除指定位置的元素时:
返回值是一个迭代器,指向删除元素下一个元素;
如果是删除某范围内的元素时:返回值也表示一个迭代器,指向最后一个删除元素的下一个元素;


看下面的程序,目的是删除数组里面的所有值为6的元素:

  1. #include<iostream>  
  2. #include<vector>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     vector<int> array;  
  7.     array.push_back(1);  
  8.     array.push_back(6);  
  9.     array.push_back(3);  
  10.     array.push_back(6);  
  11.     array.push_back(6);  
  12.     array.push_back(2);  
  13.   
  14.     vector<int>::iterator itor;  
  15.     vector<int>::iterator itor2;  
  16.     for(itor=array.begin();itor!=array.end();)  
  17.     {  
  18.         if(6==*itor)  
  19.         {  
  20.            itor2=itor;  
  21.            array.erase(itor2);  
  22.   
  23.         }  
  24.         itor++;  
  25.     }  
  26.     itor=array.begin();  
  27.     for(itor=array.begin();itor!=array.end();)  
  28.     {  
  29.         cout<<(*itor++);  
  30.     }  
  31.     getchar();  
  32.     return 0;  
  33. }  


运行结果输出1362,可见其中一个6并未删除,这是迭代器的问题。
原因在于erase以后,itor已经指向下一个元素了,不应该在itor++,否则会跳过下一个元素,即连续两个6时跳过了第二个6.
另外,在itor2=itor时,两个itor是一样的,这样做并无意义。可修改如下:

  1. vector<int>::iterator itor;  
  2. // vector<int>::iterator itor2;  
  3. for(itor=array.begin();itor!=array.end();)  
  4. {  
  5.     if(6==*itor)  
  6.     {  
  7.         // itor2=itor;  
  8.         array.erase(itor);  
  9.     }  
  10.     else  
  11.     {  
  12.         itor++;  
  13.     }  
  14. }  


或者

  1. vector<int>::iterator itor;  
  2. for(itor=array.begin();itor!=array.end();itor++)  
  3. {  
  4.     if(6==*itor)  
  5.     {  
  6.         array.erase(itor);  
  7.         itor--;  
  8.     }  
  9. }  

也可以使用remove方法:
array.earse( remove(array.begin(), array.end(),6),  array.end() );
使用的是myeclipse2015 stable2,maven3.3.9,SDk1.7 现在在命令行下执行mvn -v 版本信息正确; 但是直接执行mvn错误了,信息如下: C:\Users\dansan>mvn [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.121 s [INFO] Finished at: 2016-07-09T20:11:49+08:00 [INFO] Final Memory: 5M/152M [INFO] ------------------------------------------------------------------------ [ERROR] No goals have been specified for this build. You must specify a valid li fecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id >:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are : validate, initialize, generate-sources, process-sources, generate-resources, p rocess-resources, compile, process-classes, generate-test-sources, process-test- sources, generate-test-resources, process-test-resources, test-compile, process- test-classes, test, prepare-package, package, pre-integration-test, integration- test, post-integration-test, verify, install, deploy, pre-clean, clean, post-cle an, pre-site, site, post-site, site-deploy. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit ch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please rea d the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifie dException 这个到底是什么问题,已经在网络上找了好多问题,都没有解决
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页