Remove(delete) all elements in the vector : vector erase « vector « C++ Tutorial






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

#include <vector>    // vector class-template definition
#include <algorithm> // copy algorithm
#include <iterator>  // ostream_iterator iterator
#include <stdexcept> // out_of_range exception

int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 };
   std::vector< int > integers( array, array + 6 );
   std::ostream_iterator< int > output( cout, " " );

   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );

   cout << "Vector integers contains: ";
   std::copy( integers.begin(), integers.end(), output );

   // erase remaining elements
   integers.erase( integers.begin(), integers.end() );
   cout << "\nAfter erasing all elements, vector integers "
      << ( integers.empty() ? "is" : "is not" ) << " empty";

   return 0;
}
Vector integers contains: 1 2 3 4 5 6 2 3 4
After erasing all elements, vector integers is empty








16.13.vector erase
16.13.1.Remove(delete) all elements in the vector
16.13.2.Erase first element
16.13.3.erase the numbers 2 through 5 in v1
16.13.4.insert and erase.
16.13.5.Use unique_copy to remove duplicate words
16.13.6.Erase adjacent duplicates
16.13.7.Erase all value in a vector more than three standard deviations greater than the mean
16.13.8.Erase all value in a vector more than three standard deviations less than the mean with erase() remove_if() and bind2nd