Re: std::make_heap - compilation issues ?
Lady_A wrote:
Hi,
I have a set with a custom ordering operator defined on it.
My goal is get 10 max elements using another ordering.
To do that I am thinking to use make_heap algorithm on the set,
supplying the begin and end iterators of the set and a new ordering
operator.
You can't do that - the ordering of a set is a fundamental invariant of
the set, and you can't alter it once you have created the set. Instead,
you're going to have to copy the set to another container (I'd recommend
a std::vector). e.g.
set<A*, op1>* my_set; //why a pointer?
void B:my_func() {
std::vector<A*> v;
v.reserve(my_set->size());
v.insert(v.end(), my_set->begin(), my_set()->end());
make_heap(v.begin(), v.end(), op2());
}
However, if you want the 10 maximum elements, use this:
typedef std::vector<A*>::const_iterator it;
std::vector<A*> B::getTopN(size_t N) {
std::vector<A*> v;
v.reserve(my_set->size());
v.insert(v.end(), my_set->begin(), my_set()->end());
int pos = std::min(v.size(), N);
std::nth_element(v.begin(), v.begin() + pos, v.end(), op2());
v.resize(pos);
return v; //v contains top N elements.
}
or similar.
Why do I need operator- ? Operator for comparing is not enough ?
make_heap needs random access iterators.
Tom