从列表中删除所有连续的重复元素| C ++ STL
给定包含某些元素的列表,我们必须从C++STL中的列表中删除连续的重复元素。
list.unique()函数
在C++STL中,要删除连续的重复元素,我们使用库函数“list”头,即unique()该函数从列表中删除所有连续的重复元素(也类似于其他容器)。我们得到的列表没有连续的重复元素。
示例
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程序:
#include <iostream>
#include <list>
using namespace std;
//显示列表的功能
void dispList(list<int> L)
{
//在列表中声明迭代器
list<int>::iterator l_iter;
for (l_iter = L.begin(); l_iter != L.end(); l_iter++)
cout<< *l_iter<< " ";
cout<<endl;
}
int main(){
//声明列表
list<int> iList = {10, 10, 20, 30, 30, 10, 20, 40, 40, 50};
//打印列表元素
cout<<"List elements are"<<endl;
dispList(iList);
//删除所有连续的重复元素
iList.unique();
cout<<"List after removing consecutive duplicate elements"<<endl;
dispList(iList);
return 0;
}输出结果
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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短