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






Use std::remove to delete all element in a vector by value

  
 

#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 
 */
        
    
  








Related examples in the same category

1.Use the generic remove algorithm
2.Remove an element and then erase that element
3.Combine remove and erase together
4.Remove all instances of '0'
5.Remove copy Elements if They Meet a Criterion
6.remove empty string