STL_set——set::erase

Reference:
Removes an element or a range of elements in a set from specified positions or removes elements that match a specified key.

Function:
iterator erase(
   iterator _Where
);
iterator erase(
   iterator _First,
   iterator _Last
);
size_type erase(
   const key_type& _Key
);

Parameters:
_Where:Position of the element to be removed from the set.
_First:Position of the first element removed from the set.
_Last:Position just beyond the last element removed from the set.
_Key:The key of the elements to be removed from the set.

Return Value:
For the first two member functions, a bidirectional iterator that designates the first element remaining beyond any elements removed, or a pointer to the end of the set if no such element exists. For the third member function, the number of elements that have been removed from the set.

Remarks:
The member functions never throw an exception.

Example:
#include <set>
#include <iostream>

int main( )
{
   using namespace std;   
   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;
}

Output:
After the 2nd element is deleted, the set s1 is: 1 3 4.
After the middle two elements are deleted, the set s2 is: 1 16.
After the element with a key of 2 is deleted, the set s3 is: 0 1 3.
The number of elements removed from s3 is: 1.
After another element (unique for set) with a key
equal to that of the 2nd element is deleted, the set s3 is: 0 3.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值