This example code is explain in A Billing System for Phone Calls (stxxl::vector and stxxl::sort)
#include <stxxl.h>
#include <fstream>
#include <ctime>
struct LogEntry
{
long long unsigned from;
long long unsigned to;
int event;
};
struct cmp
{
bool operator () (const LogEntry& a, const LogEntry& b) const
{
return a.timestamp < b.timestamp;
}
static LogEntry min_value()
{
LogEntry e;
return e;
}
static LogEntry max_value()
{
LogEntry e;
return e;
}
};
std::ostream&
operator << (std::ostream& i,
const LogEntry& entry)
{
i << entry.from << " ";
i << entry.to << " ";
i << entry.timestamp << " ";
i << entry.event << "\n";
return i;
}
int main(int argc, char* argv[])
{
if (argc < 5)
{
STXXL_MSG(
"Usage: " << argv[0] <<
" ncalls avcalls main logfile");
STXXL_MSG(
" avcalls - average number of calls per client");
STXXL_MSG(
" logfile - file name of the output");
return 0;
}
const long av_calls = atol(argv[2]);
time_t now = time(NULL);
vector_type log;
log.reserve(2 * ncalls);
number < nclients && calls_made < ncalls;
++number)
{
long long int serv = std::min<long long int>(rnd(av_calls * 2), (ncalls - calls_made));
LogEntry e;
e.from = number;
time_t cur = now;
while (serv-- > 0)
{
cur += (time_t)(1 + rnd(3600 * 24));
e.to = rnd(nclients);
e.timestamp = cur;
++calls_made;
e.event = 1;
log.push_back(e);
cur += (time_t)(1 + rnd(1800));
e.timestamp = cur;
e.event = 2;
log.push_back(e);
}
}
std::fstream out(argv[4], std::ios::out);
std::copy(log.begin(), log.end(), std::ostream_iterator<LogEntry>(out));
STXXL_MSG(
"\n" << calls_made <<
" calls made.");
STXXL_MSG(
"The log is written to '" << argv[4] <<
"'.");
return 0;
}