共用方式為


list::erase

從指定的位置移除項目或某個範圍的項目清單中的。

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

參數

  • _Where
    從清單中移除之項目的位置。

  • _First
    從 清單中移除第一個項目的位置。

  • _Last
    在 清單中移除最後一個項目以外的位置。

傳回值

指定保持在所有項目外的第一個項目中移除的雙向 Iterator 或到結尾的指標的清單,如果沒有此類項目存在則為。

備註

轉散發,不會發生,因此 Iterator 和參考就會變成無效只為了清除的項目。

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

標準樣板程式庫