• <small id='qzcZ2'></small><noframes id='qzcZ2'>

  • <legend id='qzcZ2'><style id='qzcZ2'><dir id='qzcZ2'><q id='qzcZ2'></q></dir></style></legend>

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

        如何使向量的元素唯一?(删除不相邻的重复项)

        时间:2023-09-16

          <tfoot id='9iSac'></tfoot>

            <tbody id='9iSac'></tbody>
          • <bdo id='9iSac'></bdo><ul id='9iSac'></ul>

                  <small id='9iSac'></small><noframes id='9iSac'>

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

                  本文介绍了如何使向量的元素唯一?(删除不相邻的重复项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个包含很少非相邻重复项的向量.

                  I have a vector containing few non-adjacent duplicates.

                  举一个简单的例子,考虑:

                  As a simple example, consider:

                  2 1 6 1 4 6 2 1 1
                  

                  我试图通过删除不相邻的重复项并保持元素的顺序来使这个 vector 独一无二.

                  I am trying to make this vector unique by removing the non-adjacent duplicates and maintaining the order of elements.

                  结果是:

                  2 1 6 4 
                  

                  我尝试的解决方案是:

                  1. 插入 std::set 但这种方法的问题是它会扰乱元素的顺序.
                  2. 使用 std::sort 和 std::unique 的组合.但同样的顺序问题.
                  3. 手动消除重复:

                  1. Inserting into a std::set but the problem with this approach is that it will disturb the order of elements.
                  2. Use the combination of std::sort and std::unique. But again same order problem.
                  3. Manual duplicate elimination:

                      Define a temporary vector TempVector.
                      for (each element in a vector)
                      {
                          if (the element does not exists in TempVector)
                          {
                              add to TempVector;
                          }
                      }
                      swap orginial vector with TempVector.
                  

                  我的问题是:

                  是否有任何 STL 算法可以从向量中删除不相邻的重复项?它的复杂程度如何?

                  Is there any STL algorithm which can remove the non-adjacent duplicates from the vector ? what is its complexity?

                  推荐答案

                  不使用临时 set 就可以做到这一点,但(可能)会损失一些性能:

                  Without using a temporary set it's possible to do this with (possibly) some loss of performance:

                  template<class Iterator>
                  Iterator Unique(Iterator first, Iterator last)
                  {
                      while (first != last)
                      {
                          Iterator next(first);
                          last = std::remove(++next, last, *first);
                          first = next;
                      }
                  
                      return last;
                  }
                  

                  用于:

                  vec.erase( Unique( vec.begin(), vec.end() ), vec.end() );
                  

                  对于较小的数据集,实现的简单性和不需要额外的分配可能会抵消使用额外的set 理论上更高的复杂性.不过,使用具有代表性的输入进行测量是唯一确定的方法.

                  For smaller data sets, the implementation simplicity and lack of extra allocation required may offset the theoretical higher complexity of using an additional set. Measurement with a representative input is the only way to be sure, though.

                  这篇关于如何使向量的元素唯一?(删除不相邻的重复项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在二进制文件中读取/写入结构? 下一篇:我如何使用 std::vector&lt;std::mutex&gt; 之类的

                  相关文章

                  最新文章

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

                    1. <small id='gXOGG'></small><noframes id='gXOGG'>

                    2. <legend id='gXOGG'><style id='gXOGG'><dir id='gXOGG'><q id='gXOGG'></q></dir></style></legend>
                      <tfoot id='gXOGG'></tfoot>