在vector中,怎样删除某个指定值的元素

【在vector中,怎样删除某个指定值的元素】
Vectors provide no operation to remove elements directly that have a certain value. You must use an algorithm to do this.
一.删除所有满足条件的元素
For example, the following statement removes all elements that have the value val:
   std::vector coll;
   ...
   //remove all elements with value val
   coll.erase(remove(coll.begin(),coll.end(),
                     val),
              coll.end());
二.只删除第一个
To remove only the first element that has a certain value, you must use the following statements:
   std::vector coll;
   ...
   //remove first element with value val
   std::vector::iterator pos;
   pos = find(coll.begin(),coll.end(),
              val);
   if (pos != coll.end()) {
       coll.erase(pos);
   }

你可能感兴趣的:(STL,c++)