Sort a std::list with myclass::operator<(myclass &other)

后端 未结 5 647
一生所求
一生所求 2021-01-12 04:31

I have a std::list and in my class I have myclass::operator<(myclass &other) defined.

I use the std::list.sort

5条回答
  •  感动是毒
    2021-01-12 05:26

    assuming you don't have NULL pointers in your list just do

    bool ptrsorter( myclass *a, myclass *b ) {
      return *a < *b;
      }
    
    mylist.sort( ptrsorter );
    

    or if you're lucky enough to be using a more recent compiler (with C++0x-support), you can use a lambda-expression:

    mylist.sort( []( myclass *a, myclass *b ) { return *a < *b } );
    

提交回复
热议问题