简体   繁体   English

STXXL:如何对第二个元素上的对向量进行排序?

[英]STXXL: How to sort Vector of pairs on second element?

Similar Question is available here: How do I sort a vector of pairs based on the second element of the pair? 这里有类似的问题: 如何根据对的第二个元素对向量进行排序? but I am interested in External Memory Sorting. 但我对外部存储器排序感兴趣。

I have tried using the analogies from Internal Memory Sorting but the error occurs in sorter_stream.h file of STXXL as: 我尝试使用内部存储器排序中的类比,但是在STXXL的sorter_stream.h文件中出现错误,如下所示:

My code : 我的代码:

#include <iostream>
#include <stxxl/vector>
#include <stxxl/sorter>
#include <limits>
using namespace std;

typedef std::pair<int,int> my_pair;

struct my_comparator
{
    bool operator()(const my_pair& left, const my_pair& right)
    {
        return left.first < right.first;
    }
    int min_value() const
    {
        return std::numeric_limits<int>::min();
    }
    int max_value() const
    {
        return std::numeric_limits<int>::max();
    }
};
int main()
{
    typedef stxxl::sorter<my_pair, my_comparator> sorter_type;
    sorter_type int_sorter(my_comparator(), 64 * 1024 * 1024);
    for (int i = 10; i > 0; i--)
    {
        int_sorter.push(my_pair(i,i+10));
    }
    int_sorter.sort();  // sort elements (in ascending order)
    while (!int_sorter.empty())
    {
        std::cout << (*int_sorter).first << " "<<(*int_sorter).second<<endl;
        ++int_sorter;
    }
    return 0;
}

Error : 错误:

sort_stream.h(481): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

UPDATE: 更新:

Changing the return type of min_value(),max_value() function to my_pair as: 将min_value(),max_value()函数的返回类型更改为my_pair,方法是:

struct my_comparator
{
    bool operator()(const my_pair& left, const my_pair& right)
    {
        return left.first < right.first;
    }

    my_pair min_value() const
    {
        return my_pair(std::numeric_limits<int>::min(),std::numeric_limits<int>::min());
    }
    my_pair max_value() const
    {
        return my_pair(std::numeric_limits<int>::max(),std::numeric_limits<int>::max());
    }

};

gives the following Error: 给出以下错误:

sort_helper.h(94): error C3848: expression having type 'const my_comparator' would lose some const-volatile qualifiers in order to call 'bool my_comparator::operator ()(const my_pair &,const my_pair &)'

PS : Being a novice (Reputation<50) , I am not allowed to comment, that's why writing a new Question. PS:作为一个新手(Reputation <50),我不允许发表评论,这就是为什么要写一个新问题。

Got the following example in STXXL:Sorter Section which addresses the same problem. 在STXXL:Sorter部分中获得了以下示例,该示例解决了相同的问题。

Code: 码:

#include <stxxl/sorter>
#include <stxxl/stats>
#include <stxxl/timer>
#include <stxxl/random>
#include <limits>
struct TwoInteger
{
    int i, j;
    TwoInteger()
    { }
    TwoInteger(int _i, int _j)
        : i(_i), j(_j)
    { }
};
struct TwoIntegerComparator
{
    bool operator () (const TwoInteger& a, const TwoInteger& b) const
    {
        return a.i < b.i;
    }
    TwoInteger min_value() const
    {
        return TwoInteger(std::numeric_limits<int>::min(), std::numeric_limits<int>::min());
    }
    TwoInteger max_value() const
    {
        return TwoInteger(std::numeric_limits<int>::max(), std::numeric_limits<int>::max());
    }
};
int main()
{
    // template parameter <ValueType, CompareType, BlockSize(optional), AllocStr(optional)>
    typedef stxxl::sorter<TwoInteger, TwoIntegerComparator, 1*1024*1024> sorter_type;
    // create sorter object (CompareType(), MainMemoryLimit)
    sorter_type int_sorter(TwoIntegerComparator(), 64 * 1024 * 1024);
    stxxl::random_number32 rand32;
    stxxl::timer Timer1;
    Timer1.start();
    // insert random numbers from [0,100000)
    for (size_t i = 0; i < 1000; ++i)
    {
        int_sorter.push(TwoInteger(rand32() % 100000, (int)i));    // fill sorter container
    }
    Timer1.stop();
    STXXL_MSG("push time: " << (Timer1.mseconds() / 1000));
    stxxl::timer Timer2;
    Timer2.start();
    int_sorter.sort();  // switch to output state and sort
    Timer2.stop();
    STXXL_MSG("sort time: " << (Timer2.mseconds() / 1000));
    // echo sorted elements
    while (!int_sorter.empty())
    {
        std::cout << int_sorter->i << " ";  // access value
        ++int_sorter;
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM