Use std::remove to delete all element in a vector by value : remove « STL Algorithms Modifying sequence operations « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>
#include <vector>
#include <iterator>

int main()
{
   int a[ 10 ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 };

   std::ostream_iterator< int > output( cout, " " );

   std::vector< int > v( a, a + 10 ); // copy of a

   std::vector< int >::iterator newLastElement;

   cout << "Vector v before removing all 10s:\n   ";
   std::copy( v.begin(), v.end(), output );

   // remove all 10s from v
   newLastElement = std::remove( v.begin(), v.end(), 10 );
   cout << "\nVector v after removing all 10s:\n   ";
   std::copy( v.begin(), newLastElement, output );

   return 0;
}
Vector v before removing all 10s:
   10 2 10 4 16 6 14 8 12 10
Vector v after removing all 10s:
   2 4 16 6 14 8 12








24.8.remove
24.8.1.Use the generic remove algorithm
24.8.2.Use std::remove to delete all element in a vector by value
24.8.3.Remove an element and then erase that element
24.8.4.Combine remove and erase together
24.8.5.Use remove() to delete elements from a vector
24.8.6.std::remove does not change the size of the container,it moves elements forward to fill gaps created and returns the new 'end' position.
24.8.7.Remove value from a vector with remove()