std::list::sort() - on interfaces
Anyway, back to the original post. If you are using MSVC 6, and can''t use std::list::sort properly, one idea is to store the Shape pointers in a std::map. std::map allows you to specify the sorting property, and I think MSVC 6 implements it properly. Then when all the Shapes are registered, iterate through the map and move the Shape pointers to the list. You might be able to do the same thing with a std:riority_queue instead of a std::map as well. Or store the pointers in a std::vector, apply std::sort and then copy into the list. And so on.
well im using VS 7.0,
i get the following error:
error code:
error C2275: 'ShapeCMP
Any ideas ?
[edited by - LogouT on December 15, 2003 5:17:53 PM]
i get the following error:
shapes.sort( < Shape* > ); //<- i get the compiler error here
error code:
error C2275: 'ShapeCMP
' : illegal use of this type as an expression
with
[
P=Shape *
]
and here is how the ShapeCMP struc is made:
template class ShapeCMP : public binary_function< P, P, bool>{ bool operator()(P p1, P p2) const { return (*p1.nSize < *p2.nSize); }};
Any ideas ?
[edited by - LogouT on December 15, 2003 5:17:53 PM]
quote:Original post by Sneftel
IIRC, std::sort won''t work on std::list because it expects bidirectional iterators.
No, it needs Random Access.
Even if it didn''t, it''d still be better to use std::list::sort, because std::list::sort can take advantage of the ability to merge sort linked lists with no temporary storage (which requires some trickery with arrays, though I think can be implemented).
You''ve got several errors with that code.
For one, ShapeCMP::operator() isn''t public. Also in ShapeCMP::operator(), there''s a precendence error with dereferencing the pointers. Finally, the call to std::list::sort() isn''t correct. Try:
shapes.sort<ShapeCMP<Shape *> >(ShapeCMP<Shape *>());
For one, ShapeCMP::operator() isn''t public. Also in ShapeCMP::operator(), there''s a precendence error with dereferencing the pointers. Finally, the call to std::list::sort() isn''t correct. Try:
shapes.sort<ShapeCMP<Shape *> >(ShapeCMP<Shape *>());
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement