<legend id='FcKPi'><style id='FcKPi'><dir id='FcKPi'><q id='FcKPi'></q></dir></style></legend>

<i id='FcKPi'><tr id='FcKPi'><dt id='FcKPi'><q id='FcKPi'><span id='FcKPi'><b id='FcKPi'><form id='FcKPi'><ins id='FcKPi'></ins><ul id='FcKPi'></ul><sub id='FcKPi'></sub></form><legend id='FcKPi'></legend><bdo id='FcKPi'><pre id='FcKPi'><center id='FcKPi'></center></pre></bdo></b><th id='FcKPi'></th></span></q></dt></tr></i><div id='FcKPi'><tfoot id='FcKPi'></tfoot><dl id='FcKPi'><fieldset id='FcKPi'></fieldset></dl></div>
      <bdo id='FcKPi'></bdo><ul id='FcKPi'></ul>

    <small id='FcKPi'></small><noframes id='FcKPi'>

  • <tfoot id='FcKPi'></tfoot>

        从列表中删除重复项<int>

        时间:2024-05-12
        <tfoot id='FTLdy'></tfoot>

            • <i id='FTLdy'><tr id='FTLdy'><dt id='FTLdy'><q id='FTLdy'><span id='FTLdy'><b id='FTLdy'><form id='FTLdy'><ins id='FTLdy'></ins><ul id='FTLdy'></ul><sub id='FTLdy'></sub></form><legend id='FTLdy'></legend><bdo id='FTLdy'><pre id='FTLdy'><center id='FTLdy'></center></pre></bdo></b><th id='FTLdy'></th></span></q></dt></tr></i><div id='FTLdy'><tfoot id='FTLdy'></tfoot><dl id='FTLdy'><fieldset id='FTLdy'></fieldset></dl></div>

            • <legend id='FTLdy'><style id='FTLdy'><dir id='FTLdy'><q id='FTLdy'></q></dir></style></legend>
                <tbody id='FTLdy'></tbody>
                <bdo id='FTLdy'></bdo><ul id='FTLdy'></ul>

                  <small id='FTLdy'></small><noframes id='FTLdy'>

                  本文介绍了从列表中删除重复项<int>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 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:

                  listl;

                  请注意,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>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 (i|o)fstream 为文件名采用 const char* 参数? 下一篇:stl 随机分布和可移植性

                  相关文章

                  <small id='skDS9'></small><noframes id='skDS9'>

                1. <legend id='skDS9'><style id='skDS9'><dir id='skDS9'><q id='skDS9'></q></dir></style></legend>

                    • <bdo id='skDS9'></bdo><ul id='skDS9'></ul>
                    <tfoot id='skDS9'></tfoot>

                  1. <i id='skDS9'><tr id='skDS9'><dt id='skDS9'><q id='skDS9'><span id='skDS9'><b id='skDS9'><form id='skDS9'><ins id='skDS9'></ins><ul id='skDS9'></ul><sub id='skDS9'></sub></form><legend id='skDS9'></legend><bdo id='skDS9'><pre id='skDS9'><center id='skDS9'></center></pre></bdo></b><th id='skDS9'></th></span></q></dt></tr></i><div id='skDS9'><tfoot id='skDS9'></tfoot><dl id='skDS9'><fieldset id='skDS9'></fieldset></dl></div>