Hi,
I am trying to use an external sort method I found in this book:
http://www.informatik.hs-bremen.de/~brey/stlbe.html
the interface is this:
Code:
int main() {
std::less<long> Comparison; // descending
// std::greater<long> Comparison; // ascending
std::istream_iterator<long> suitable_iterator;
std::cout << externalSorting(
suitable_iterator, // type of file
"random.dat", // file name
"\n", // separator
Comparison) // sorting criterion
<< " sorting runs" << std::endl;
}
and the externalSorting(...) method is
Code:
template<class IstreamIterator, class Compare>
int externalSorting(IstreamIterator& InputIterator,
const char *SortFile,
const char *Separator,
const Compare& comp) {
typedef typename std::iterator_traits<IstreamIterator>::value_type
valueType;
bool sorted = false;
// arbitrary names for auxiliary files
const char *TempFile1 = "esort001.tmp",
*TempFile2 = "esort002.tmp";
int Run = 0; // number of sort-merge cycles
do {
std::ifstream Inputfile(SortFile);
SubsequenceIterator<valueType, Compare>
FileIterator(Inputfile, comp);
split(FileIterator, TempFile1, TempFile2, sorted);
Inputfile.close();
if(!sorted) {
// prepare for merging
std::ifstream Source1(TempFile1);
std::ifstream Source2(TempFile2);
SubsequenceIterator<valueType, Compare> I1(Source1, comp);
SubsequenceIterator<valueType, Compare> I2(Source2, comp);
SubsequenceIterator<valueType, Compare> End;
// open SortFile for writing
std::ofstream Outputfile(SortFile);
std::ostream_iterator<valueType> Result(Outputfile, Separator);
mergeSubsequences(I1, End, I2, End, Result, comp);
++Run;
}
} while(!sorted);
return Run;
}
I want to sort records stored in a text file of the form
Code:
class node{
public:
long key;
string values;
};
and sort them by key.
but when I create my own comparator function, it does not compile, I am trying to do this
Code:
struct less2 : binary_function<node, node, bool> {
bool operator()(const node& x, const node& y) const { return x.key < y.key; }
};
int main() {
less2 Comparison; // descending
std::istream_iterator<node> suitable_iterator;
std::cout << externalSorting(
suitable_iterator, // type of file
"random.dat", // file name
"\n", // separator
Comparison) // sorting criterion <-- gives error, says "instantiated from here
<< " sorting runs" << std::endl;
}
Any ideas as how to fix this?
thanks