Removing values from a sequence with algorithms remove - C++ STL Algorithm

C++ examples for STL Algorithm:remove

Description

Removing values from a sequence with algorithms remove

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <vector> // vector class-template definition 
#include <iterator> // ostream_iterator 
using namespace std; 

int main() //from   www. jav  a  2 s. co m
{ 
   const int SIZE = 10; 
   int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; 
   ostream_iterator< int > output( cout, " " ); 
   vector< int > v( a, a + SIZE ); // copy of a 
   vector< int >::iterator newLastElement; 

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

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

   cout << endl; 
}

Result


Related Tutorials