C++ forward_list 庫 - remove() 函式



描述

C++ 函式std::forward_list::remove() 從 forward_list 中移除與指定值匹配的元素,並根據移除的元素數量減少 forward_list 的大小。

宣告

以下是來自 std::forward_list 標頭檔案的 std::forward_list::remove() 函式宣告。

C++11

void remove (const value_type& val);

引數

val − 要移除的元素的值。

返回值

異常

此成員函式從不丟擲異常。

時間複雜度

此成員函式從不丟擲異常。

示例

以下示例演示了 std::forward_list::remove() 函式的用法。

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl = {1, 2, 2, 3, 3, 3, 4, 5};

   cout << "List contents before remove operation" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   fl.remove(2);

   cout << "List contents after remove operation" << endl;

   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

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

List contents before remove operation
1
2
2
3
3
3
4
5
List contents after remove operation
1
3
3
3
4
5
forward_list.htm
廣告
© . All rights reserved.