C++ 列表庫 - erase_range() 函式



描述

C++ 函式std::list::erase_range() 從列表中移除一段元素,並修改列表的大小。

宣告

以下是來自 `std::list` 標頭檔案的 `std::list::erase_range()` 函式宣告。

C++98

iterator erase (iterator first, iterator last);

C++11

iterator erase (const_iterator first, const_iterator last);

引數

  • first − 輸入迭代器,指向範圍的起始位置。

  • last − 輸入迭代器,指向範圍的結束位置。

返回值

返回一個隨機訪問迭代器。

異常

如果範圍無效,則行為未定義。

時間複雜度

線性,即 O(n)

示例

以下示例演示了 `std::list::erase_range()` 函式的使用方法。

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l = {1, 2, 3, 4, 5};

   cout << "Size of list befor erase operation = " << l.size() << endl;

   l.erase(l.begin(), l.end());

   cout << "Size of list after erase operation = " << l.size() << endl;

   return 0;
}

讓我們編譯並執行上面的程式,這將產生以下結果:

Size of list befor erase operation = 5
Size of list after erase operation = 0
list.htm
廣告
© . All rights reserved.