erase from begin() to end() : multiset erase « set multiset « C++ Tutorial






#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const int num_years = 5;
   const char* d[num_years] = { "A", "B","C", "D", "E" };
   const char* d2[num_years] = { "a", "b","c", "d", "e" };
   const char* d3[num_years] = { "1","2", "3",  "4", "5" };

   multiset<string> s1( d,d+num_years );
   multiset<string> s2( d2,d2+num_years );
   multiset<string> s3( d3,d3+num_years );

   print( s1 );
   print( s2 );
   print( s3 );

   vector<string> winner;
   merge( s1.begin(), s1.end(),s2.begin(), s2.end(), back_inserter( winner ) );
   vector<string> all_winner;
   merge( winner.begin(), winner.end(),s3.begin(), s3.end(), back_inserter( all_winner ) );

   all_winner.erase( unique( all_winner.begin(), all_winner.end() ),all_winner.end() );
   cout << endl << all_winner.size() << " UNIQUE WINNERS\n";
   for( vector<string>::size_type i = 0; i < all_winner.size(); ++i )
   {
      cout << all_winner[i] << "  ";
      if( (i+1) % 5 == 0 )
         cout << endl;
   }
}








19.14.multiset erase
19.14.1.Remove all elements up to element with value 3
19.14.2.Remove all elements with value 5
19.14.3.Demonstrating multiset erase functions: erase a range
19.14.4.Demonstrating multiset erase a found element
19.14.5.erase from begin() to end()
19.14.6.Demonstrating multiset erase functions