chapter9-20.cpp

来自「C++STL程序员开发指南」· C++ 代码 · 共 43 行

CPP
43
字号
//文件名:CHAPTER9-20.cpp
#include <set>
#include <iostream>
#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
#endif
int main( )
{
   multiset <int> ms1, ms2, ms3;
   multiset <int> :: iterator pIter, Iter1, Iter2;
   int i, n;
   for ( i = 1 ; i < 5 ; i++ ) { ms1.insert ( i );  ms2.insert ( i * i );  ms3.insert ( i - 1 ); }
   // The 1st member function removes an element at a given position
   Iter1 = ++ms1.begin( );
   ms1.erase( Iter1 );
   cout << "After the 2nd element is deleted, the multiset ms1 is:" ;
   for ( pIter = ms1.begin( ) ; pIter != ms1.end( ) ; pIter++ )cout << " " << *pIter;
   cout << "." << endl;
   // The 2nd member function removes elements
   // in the range [_First, _Last)
   Iter1 = ++ms2.begin( );
   Iter2 = --ms2.end( );
   ms2.erase( Iter1, Iter2 );
   cout << "After the middle two elements are deleted, "<< "the multiset ms2 is:" ;
   for ( pIter = ms2.begin( ) ; pIter != ms2.end( ) ; pIter++ ) cout << " " << *pIter;
   cout << "." << endl;
   // The 3rd member function removes elements with a given _Key
   ms3.insert( 2 );
   n = ms3.erase( 2 );
   cout << "After the element with a key of 2 is deleted,\n"<< "the multiset ms3 is:" ;
   for ( pIter = ms3.begin( ) ; pIter != ms3.end( ) ; pIter++ ) cout << " " << *pIter;
   cout << "." << endl;
   // The 3rd member function returns the number of elements removed
   cout << "The number of elements removed from ms3 is: "<< n << "." << endl;
   // The dereferenced iterator can also be used to specify a key
   Iter1 = ++ms3.begin( );
   ms3.erase( Iter1 );
   cout << "After another element with a key"<< endl;
   cout  << "equal to that of the 2nd element is deleted, "<< "the multiset ms3 is:" ;
   for ( pIter = ms3.begin( ) ; pIter != ms3.end( ) ; pIter++ ) cout << " " << *pIter;
   cout << "." << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?