CHAPTER9-19.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:2k
- //文件名:CHAPTER9-19.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( )
- {
- set <int> s1, s2, s3;
- set <int> :: iterator pIter, Iter1, Iter2;
- int i, n;
- for ( i = 1 ; i < 5 ; i++ ) { s1.insert ( i ); s2.insert ( i * i ); s3.insert ( i - 1 ); }
- // The 1st member function removes an element at a given position
- Iter1 = ++s1.begin( );
- s1.erase( Iter1 );
- cout << "After the 2nd element is deleted, the set s1 is:" ;
- for ( pIter = s1.begin( ) ; pIter != s1.end( ) ; pIter++ ) cout << " " << *pIter;
- cout << "." << endl;
- // The 2nd member function removes elements
- // in the range [_First, _Last)
- Iter1 = ++s2.begin( );
- Iter2 = --s2.end( );
- s2.erase( Iter1, Iter2 );
- cout << "After the middle two elements are deleted, "<< "the set s2 is:" ;
- for ( pIter = s2.begin( ) ; pIter != s2.end( ) ; pIter++ ) cout << " " << *pIter;
- cout << "." << endl;
- // The 3rd member function removes elements with a given _Key
- n = s3.erase( 2 );
- cout << "After the element with a key of 2 is deleted, "<< "the set s3 is:" ;
- for ( pIter = s3.begin( ) ; pIter != s3.end( ) ; pIter++ ) cout << " " << *pIter;
- cout << "." << endl;
- // The 3rd member function returns the number of elements removed
- cout << "The number of elements removed from s3 is: "<< n << "." << endl;
- // The dereferenced iterator can also be used to specify a key
- Iter1 = ++s3.begin( );
- s3.erase( Iter1 );
- cout << "After another element (unique for set) with a key"<< endl;
- cout << "equal to that of the 2nd element is deleted, "<< "the set s3 is:" ;
- for ( pIter = s3.begin( ) ; pIter != s3.end( ) ; pIter++ ) cout << " " << *pIter;
- cout << "." << endl;
- }