📌  相关文章
📜  C++ STL中的forward_list :: unique()

📅  最后修改于: 2021-05-30 07:32:40             🧑  作者: Mango

forward_list :: unique()是C++ STL中的内置函数,可从forward_list中删除所有连续的重复元素。它使用二进制谓词进行比较。

句法:

forwardlist_name.unique(BinaryPredicate name)

参数:函数接受一个参数,该参数是一个二进制谓词,如果元素应被视为相等,则返回true。它具有以下语法:

bool name(data_type a, data_type a)

返回值:该函数不返回任何内容。

// C++ program to illustrate the
// unique() function
#include 
using namespace std;
  
// Function for binary_predicate
/*bool compare(int a, int b)
{
    return (abs(a) == abs(b));
}
// This function can also be used and passed inside
// unique(), to get the same result
*/
  
int main()
{
  
    forward_list list = { 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 2, 4, 4 };
  
    cout << "List of elements before unique operation is: ";
  
    // starts from the first element of the list to the last
    for (auto it = list.begin(); it != list.end(); ++it)
        cout << *it << " ";
  
    // unique operation on forward list
    list.unique();
    cout << "\nList of elements after unique operation is: ";
  
    // starts from the first element of the list to the last
    for (auto it = list.begin(); it != list.end(); ++it)
        cout << *it << " ";
  
    return 0;
}
输出:
List of elements before unique operation is: 1 1 1 1 2 2 2 2 3 3 3 2 4 4 
List of elements after unique operation is: 1 2 3 2 4
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”