【问题标题】:partial_sort on boost multi_index's random access index提升 multi_index 的随机访问索引上的 partial_sort
【发布时间】:2015-09-14 14:36:47
【问题描述】:

我想知道是否有办法在multi_index 的随机访问索引上使用std::partial_sortboost::partial_sort

如果我尝试使用std::patial_sort,我会收到编译器错误,暗示迭代器取消引用是 const,因此我无法编译。我知道要保持索引的顺序,所有multi_index 迭代都是 const 迭代器。但是有内部排序函数,但是 boost multi_index 中没有 partial_sort。

【问题讨论】:

    标签: c++ boost boost-multi-index


    【解决方案1】:

    随机访问迭代器提供rearrangement,但不提供直接突变。因此,请使用代理:

    Live On Coliru

    #include <boost/multi_index_container.hpp>
    #include <boost/multi_index/random_access_index.hpp>
    #include <iostream>
    
    using Record = int; // or your user-defined type, of course
    
    namespace bmi = boost::multi_index;
    using Table = bmi::multi_index_container<Record,
          bmi::indexed_by<
            bmi::random_access<bmi::tag<struct RA_index> >
          > >;
    
    int main() {
        Table table; 
        for (Record i : { 1, 7, 4, 8, 4, 3, 4, 6, 1, -3, 31 })
            table.push_back(i);
    
        // now the partial sort:
        {
            std::vector<boost::reference_wrapper<Record const> > tmp(table.begin(), table.end());
    
            std::partial_sort(tmp.begin(), tmp.begin()+8, tmp.end());
    
            // apply back to RA_index
            table.get<RA_index>().rearrange(tmp.begin());
        }
    
        std::cout << "Partially sorted: ";
        std::copy(table.begin(), table.end(), std::ostream_iterator<Record>(std::cout, " "));
    }
    

    打印

    Partially sorted: -3 1 1 3 4 4 4 6 8 7 31 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-25
      • 2011-05-26
      • 1970-01-01
      相关资源
      最近更新 更多