问题描述
使用 STL 算法(尽可能多地),例如 remove_if() 和 list::erase,是否有一种很好的方法可以从定义为的列表中删除重复项以下:
Using STL algorithms (as much as possible) such as remove_if() and list::erase, is there a nice way to remove duplicates from a list defined as following:
list
请注意,list::unique() 仅在连续元素中出现重复时才有效.就我而言,无论它们在列表中的位置如何,都必须消除所有重复项.此外,去除重复意味着在最终结果中只保留每个元素的一个副本.
Please note that list::unique() only works if duplication occurs in consecutive elements. In my case, all duplicates have to be eliminated regardless of their position in the list. Moreover, removing duplicates mean preserving only one copy of each element in the final result.
不能使用 l.sort() 后跟 l.unique() 的选项,因为这会破坏列表的顺序.
The option to l.sort() followed by l.unique() cannot be availed as that will destroy the order of the list.
推荐答案
使用 list::remove_if 成员函数、临时散列集和 lambda 表达式.
Using the list::remove_if member function, a temporary hashed set, and lambda expression.
std::list<int> l;
std::unordered_set<int> s;
l.remove_if([&](int n) {
return (s.find(n) == s.end()) ? (s.insert(n), false) : true;
});
这篇关于从列表中删除重复项<int>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)