Аналогичный вопрос можно найти здесь: How do I sort a vector of pairs based on the second element of the pair?, но меня интересует сортировка внешней памяти.STXXL: Как отсортировать вектор пар на втором элементе?
Я попытался с помощью аналогий из внутренней сортировки памяти, но ошибка происходит в sorter_stream.h файле STXXL как:
Мой код:
#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;
}
Ошибка:
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)
ОБНОВЛЕНИЕ:
Изменение типа возвращаемого 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());
}
};
дает следующую ошибку:
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 &)'
P.S. : Будучи новичком (Репутация < 50), мне не разрешают комментировать, вот почему я пишу новый вопрос.
Я никогда не использовал эту библиотеку, но я думаю, что '' min_value' и max_value' функции должны быть возвращают значение типа, 'my_pair', а не' int'. –