Remove duplicates from a list

后端 未结 3 830
情深已故
情深已故 2020-12-03 19:01

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 fol

3条回答
  •  甜味超标
    2020-12-03 19:42

    Using the list::remove_if member function, a temporary hashed set, and lambda expression.

    std::list l;
    std::unordered_set s;
    
    l.remove_if([&](int n) {
        return (s.find(n) == s.end()) ? (s.insert(n), false) : true;
    });
    

提交回复
热议问题