list::erase

从指定的位置移除元素或元素的范围列表中的。

iterator erase(
   iterator _Where
);
iterator erase(
   iterator _First,
   iterator _Last
);

参数

  • _Where
    从列表中移除的元素的位置。

  • _First
    从列表中移除的第一个元素的位置。

  • _Last
    在从列表中移除的最后一个元素之外标识。

返回值

指定保持在所有元素外的第一个元素中移除一双向迭代器或对列表的末尾的指针,如果不存在这样的元素。

备注

重新分配时,不会发生,因此迭代器和引用变为无效仅为清除的元素。

erase 永远不会引发异常。

示例

// list_erase.cpp
// compile with: /EHsc
#include <list>
#include <iostream>

int main( ) 
{
   using namespace std;
   list <int> c1;
   list <int>::iterator Iter;
   
   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 40 );
   c1.push_back( 50 );
   cout << "The initial list is:";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;

   c1.erase( c1.begin( ) );
   cout << "After erasing the first element, the list becomes:";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;
   Iter = c1.begin( );
   Iter++;
   c1.erase( Iter, c1.end( ) );
   cout << "After erasing all elements but the first, the list becomes: ";
   for (Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << " " << *Iter;
   cout << endl;
}
  

要求

标头: <list>

命名空间: std

请参见

参考

list Class

标准模板库