1. 程式人生 > >std::vector中erase函式的用法

std::vector中erase函式的用法



(已除錯)

//std::vector, erase函式的用法
void VecEraseFun()
{
 std::vector<int>  VecInt;

 for (size_t i = 1; i <= 20; i++)
 {
  VecInt.push_back(i);
 }
 std::vector<int>::iterator it = VecInt.begin();
 for (it = VecInt.begin(); it != VecInt.end();)
 {
  if (*it % 2 == 0)
  {
   it = VecInt.erase(it);//刪除元素後,後面元素自動往前移,不用挪動指                 
  }
  else
  {
   ++it;
  }
 }
}