【问题标题】:How to sort std::vector ignoring certain numbers?如何排序 std::vector 忽略某些数字?
【发布时间】:2018-05-21 11:04:20
【问题描述】:

我正在尝试对数字向量进行排序并忽略某个数字,即将它留在原处。 This answer 实际上并没有离开它被发现的元素。

例如,如果我有以下

std::vector<int> test{5, 3, 8, 4, -1, 1, 11, 9, 6};
std::sort(test.begin(), 
         std::partition(test.begin(), test.end(), [](int n)
                                                  {return n != -1;}));

将test 排序为1 3 4 5 6 8 9 11 -1。我搜索了几个小时,并修改了自定义比较器和使用std::partition,但我无法提出将test 向量分类为1 3 4 5 -1 6 8 9 11 的解决方案。 这实际上非常困难吗?

【问题讨论】:

    标签: c++ algorithm sorting c++11 stdvector


    【解决方案1】:

    根据@Bathsheba 在他的回答中提到的补救措施,并愚弄std::sort() 的谓词,可以实现如下解决方案:

    DEMO

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
        std::vector<int> test{5, 3, 8, 4, -1, 1, 11, 9, 6};
        // get the position of -1
        auto itr = std::find(test.begin(), test.end(), -1);
        // sort all elements so that -1 will be moved to end of vector
        std::sort(test.begin(), test.end(), [](const int& lhs, const int& rhs )
            {
                if( lhs == -1 ) return false;
                if( rhs == -1 ) return true;
                return lhs < rhs;
            });
    
        test.erase(test.end()-1);   //  now erase it from end
        test.insert(itr, -1);       //  insert to the earlier position
    
        for(const auto& it: test)   std::cout << it << " ";
    
        return 0;
    }
    

    【讨论】:

    • 已更新。我没想到它会这么简单地弹出。我会回避存储 itr 以防万一它失效:不确定标准对 std::sort 上的迭代器(如果有)的失效有何规定。
    • @Bathsheba: gotcha :stackoverflow.com/questions/6438086/… 上面写着所有迭代器和引用对 std::list 也不受影响
    【解决方案2】:

    是的,使用std::sort 执行此操作很棘手:您必须以某种方式欺骗比较器,将不变数插入正确的位置,如果不事先检查其他元素,这很困难。

    一个简单的补救方法是使用插入排序;当您到达时省略不合适的数字(但记录位置),并在该记录位置的末尾手动插入。

    【讨论】:

    • 是的,我认为这可能是我必须走的路,我只是想看看是否有更清洁的方法。谢谢。
    【解决方案3】:

    给定一个向量。

    • 找到要离开的元素的位置。
    • 把它换到最后。
    • Partial sort 向量(没有最后一个元素) - 所选位置之前的所有元素都将被排序,之后会有随机顺序。
    • 将元素交换回找到的位置
    • 对向量的其余部分进行排序

    代码:

    std::vector< int > data{ 5, 3, 8, 4, -1, 1, 11, 9, 6 };
    
    auto chosen_iter = std::find( data.begin(), data.end(), -1 );
    
    std::swap( *chosen_iter, *( data.end() - 1 ) );
    
    std::partial_sort( data.begin(), chosen_iter, data.end() - 1 );
    
    std::swap( *chosen_iter, *( data.end() - 1 ) );
    
    std::sort( chosen_iter + 1, data.end() );
    

    【讨论】:

      【解决方案4】:

      不将元素交换到末尾:

      • 查找元素的位置。
      • 对向量进行部分排序,直到并排除此位置,使用比较器使该元素大于向量的其他元素,以便该元素不会出现在部分排序的部分中。
      • 使用比较器从该位置到末尾对向量的其余部分进行排序,使该元素小于向量其余部分的其他元素,从而在该位置表示该元素。

      代码:

      #include <algorithm>
      #include <iostream>
      #include <vector>
      
      using namespace std;
      
      constexpr int ignored_number = 100;
      
      int main()
      {
          vector<int> test{5, 3, 8, 4, ignored_number, 1, 11, 9, 6};
      
          auto it = find(test.begin(), test.end(), ignored_number);
          partial_sort(test.begin(), it, test.end(), [](int lhs, int rhs) {
              return lhs == ignored_number ? false :
                  (rhs == ignored_number ? true : lhs < rhs);
          });
          sort(it, test.end(), [](int lhs, int rhs) {
              return rhs == ignored_number ? false :
                  (lhs == ignored_number ? true : lhs < rhs);
          });
      
          for (const auto& x: test) {
            cout << x << ' ';
          }
          cout << endl;
      }
      

      【讨论】:

      • @Saufen 对于更大的数字,我们必须为 sort() 使用自定义比较器:[](int lhs, int rhs) { return rhs == 100 ? false : (lhs == 100 ? true : lhs &lt; rhs); }
      • 正如我在之前的评论中所说,如果数字更大, sort() 应该有一个自定义比较器(第三个参数)。我修改了我的答案以处理忽略数字的不同值:ideone.com/1eyZkV
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多