當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Forward_list unique()用法及代碼示例


描述

C++ 函數std::forward_list::unique()從 forward_list 中刪除所有連續的重複元素。它用運算符==用於比較。

聲明

以下是 std::forward_list::unique() 函數形式 std::forward_list 頭文件的聲明。

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

相關用法


注:本文由純淨天空篩選整理自 C++ Forward_list Library - unique() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。