c ++中stl
Given a list with some of the elements and we have to remove consecutive duplicate elements from the list in C++ STL.
给定包含某些元素的列表,我们必须从C ++ STL中的列表中删除连续的重复元素。
list.unique()函数 (list.unique() function)
In C++ STL, to remove consecutive duplicate elements, we use a library function of "list" header, that is unique(), this function removes all consecutive duplicate elements from the list (similar to other containers also). And we get the list without consecutive duplicate elements.
在C ++ STL中,要删除连续的重复元素,我们使用库函数“ list” header ,即unique() ,此函数从列表中删除所有连续的重复元素(也类似于其他容器)。 我们得到的列表没有连续的重复元素。
Example:
例:
Input:
List = {10, 10, 20, 30, 30, 10, 20, 40, 40, 50}
Output:
List elements are
10 10 20 30 30 10 20 40 40 50
List after removing consecutive duplicate elements
10 20 30 10 20 40 50
Program:
程序:
#include <iostream>
#include <list>
using namespace std;
//function to display the list
void dispList(list<int> L)
{
//declaring iterator to the list
list<int>::iterator l_iter;
for (l_iter = L.begin(); l_iter != L.end(); l_iter++)
cout<< *l_iter<< " ";
cout<<endl;
}
int main()
{
//declaring a list
list<int> iList = {10, 10, 20, 30, 30, 10, 20, 40, 40, 50};
//printing list elements
cout<<"List elements are"<<endl;
dispList(iList);
//remove all consecutive duplicate elements
iList.unique();
cout<<"List after removing consecutive duplicate elements"<<endl;
dispList(iList);
return 0;
}
Output
输出量
List elements are
10 10 20 30 30 10 20 40 40 50
List after removing consecutive duplicate elements
10 20 30 10 20 40 50
翻译自: https://www.includehelp.com/stl/remove-all-consecutive-duplicate-elements-from-the-list.aspx
c ++中stl