std::list::sort() - on interfaces

Started by
12 comments, last by logout 20 years, 3 months ago
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.
Advertisement
well im using VS 7.0,

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).
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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 *>());

This topic is closed to new replies.

Advertisement