C++ forward_list 庫 - unique() 函式



描述

C++函式std::forward_list::unique()移除forward_list中所有連續的重複元素。它使用operator==進行比較。

宣告

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

C++11

void unique();

引數

返回值

異常

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

時間複雜度

線性,即O(n)

示例

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

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

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

   cout << "List elements before unique operation" << endl;
   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   fl.unique();

   cout << "List elements after unique operation" << endl;
   for (auto it = fl.begin(); it != fl.end(); ++it)
      cout << *it << endl;

   return 0;
}

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

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