Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to sort std::list&ltCMyClass*&gt?

How to sort std::list&ltCMyClass*&gt?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comtutorial
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Weagle
    wrote on last edited by
    #1

    Hello all... I'm a newbie struggling with the STL and have a question about how to sort std::list<CMyBaseClass*> objects. If I call list.sort(), CMyClass::operator<(const CMyClass &rhs) is never called, so I'm thinking that the pointers are causing problems. Is there a way I can sort this list of objects by defining a bool CMyClass::Compare function? Thanks for any information. -- ________________________ matt weagle matt_weagle@nospam.hotmail.com

    E M 2 Replies Last reply
    0
    • M Matt Weagle

      Hello all... I'm a newbie struggling with the STL and have a question about how to sort std::list<CMyBaseClass*> objects. If I call list.sort(), CMyClass::operator<(const CMyClass &rhs) is never called, so I'm thinking that the pointers are causing problems. Is there a way I can sort this list of objects by defining a bool CMyClass::Compare function? Thanks for any information. -- ________________________ matt weagle matt_weagle@nospam.hotmail.com

      E Offline
      E Offline
      Erik Funkenbusch
      wrote on last edited by
      #2

      Kind of. List has 2 sort functions, not just one. The first sorts the objects by value, meaning that in your example it's just sorting the pointers. The second takes a template function called greater, which can be used to create a custom sort function by defining your own. There is also std::sort, which will also work.

      1 Reply Last reply
      0
      • M Matt Weagle

        Hello all... I'm a newbie struggling with the STL and have a question about how to sort std::list<CMyBaseClass*> objects. If I call list.sort(), CMyClass::operator<(const CMyClass &rhs) is never called, so I'm thinking that the pointers are causing problems. Is there a way I can sort this list of objects by defining a bool CMyClass::Compare function? Thanks for any information. -- ________________________ matt weagle matt_weagle@nospam.hotmail.com

        M Offline
        M Offline
        Michael S Scherotter
        wrote on last edited by
        #3

        You can try this: std::list theList; bool Less(const CMyBaseClass* pFirst, const CMyBaseClass* pSecond) { return (*pFirst < *pSecond); } std::sort(theList.begin(), theList.end(), Less); Michael S. Scherotter Lead Software Architect Tartus Development, Inc.

        A T 2 Replies Last reply
        0
        • M Michael S Scherotter

          You can try this: std::list theList; bool Less(const CMyBaseClass* pFirst, const CMyBaseClass* pSecond) { return (*pFirst < *pSecond); } std::sort(theList.begin(), theList.end(), Less); Michael S. Scherotter Lead Software Architect Tartus Development, Inc.

          A Offline
          A Offline
          Andy Hassall
          wrote on last edited by
          #4

          template<class Pred> void sort(greater<T> pr); So you want the predicate reversed, surely? std::list theList // function object, a bit more 'STL-style' class CMyBaseClassGreater { public: bool operator()(CMyBaseClass* pFirst, CMyBaseClass* pSecond) { return *pFirst > *pSecond; } } theList.sort(CMyBaseClassGreater()); std::sort won't work on a list, as it needs random access iterators, which list doesn't provide. You have to use std::list::sort().

          M 1 Reply Last reply
          0
          • A Andy Hassall

            template<class Pred> void sort(greater<T> pr); So you want the predicate reversed, surely? std::list theList // function object, a bit more 'STL-style' class CMyBaseClassGreater { public: bool operator()(CMyBaseClass* pFirst, CMyBaseClass* pSecond) { return *pFirst > *pSecond; } } theList.sort(CMyBaseClassGreater()); std::sort won't work on a list, as it needs random access iterators, which list doesn't provide. You have to use std::list::sort().

            M Offline
            M Offline
            Matt Weagle
            wrote on last edited by
            #5

            Thanks for the info. I actually got as far as figuring out the function object, but still couldn't get it to compile. I kept getting C2664 errors, complaining that it couldn't convert the function object to the proper type. Such as: error C2664: 'void __thiscall std::list<class CMyClass*,class std::allocator<class CMyClass*> >::sort(struct std::greater<class CMyClass *>)' : cannot convert parameter 1 from 'struct ObjectComp' in 'struct std:greater<class CMyClass*>' From digging through dejanews, I ran across a couple of threads that suggested that SP4 breaks template behavior of list::sort. Basically saying that the code snippet suggested works fine on gcc and Dinkumware's STL etc., but not on MSVC SP4. Is this possible? One day I will grow to like the STL :cool:

            1 Reply Last reply
            0
            • M Michael S Scherotter

              You can try this: std::list theList; bool Less(const CMyBaseClass* pFirst, const CMyBaseClass* pSecond) { return (*pFirst < *pSecond); } std::sort(theList.begin(), theList.end(), Less); Michael S. Scherotter Lead Software Architect Tartus Development, Inc.

              T Offline
              T Offline
              Tim Burke
              wrote on last edited by
              #6

              Thanks for the suggestion but that is something that I had tried until I realized that you can't use the sort algorithm for a list container. The example you showed only works for random access containers. Thanks

              J 1 Reply Last reply
              0
              • T Tim Burke

                Thanks for the suggestion but that is something that I had tried until I realized that you can't use the sort algorithm for a list container. The example you showed only works for random access containers. Thanks

                J Offline
                J Offline
                Jonathan Gilligan
                wrote on last edited by
                #7

                You can sort using any predicate function. Here are some examples from code I am working with: struct greater_magnitude { bool operator()(const CTFARect& x, const CTFARect& y) const { return (fabs(x.Value()) > fabs(y.Value())); } }; std::list m_lstRects; ... m_lstRects.sort(greater_magnitude()); In your case, you'd define struct my_comparison_predicate with member bool my_comparison_predicate::operator()(const CTFARect *x, const CTFARect * y) const after my example above. But to use this, you have to fix some bugs in the Microsoft standard C++ library. Look at Dunkumware's list of bug fixes for the VC++ standard library. Edit the standard library headers in "Program Files\Microsoft Visual Studio\vc98\include" to implement the fixes on the Dinkumware page. Then, you must edit the file "list" thus to allow the use of predicates:

                *** list	Tue Jun 27 23:50:23 2000
                --- list.original	Mon Jun 15 05:00:00 1998
                ***************
                *** 281,288 ****
                  				erase(_F++);
                  			else
                  				++_F; }
                ! //	typedef binder2nd<not_equal_to<_Ty> > _Pr1;		// Fixed 2000 06 27 JMG
                ! 	template<class _Pr1>	void remove_if(_Pr1 _Pr)	// Fixed 2000 06 27 JMG
                  		{iterator _L = end();
                  		for (iterator _F = begin(); _F != _L; )
                  			if (_Pr(*_F))
                --- 281,288 ----
                  				erase(_F++);
                  			else
                  				++_F; }
                ! 	typedef binder2nd<not_equal_to<_Ty> > _Pr1;
                ! 	void remove_if(_Pr1 _Pr)
                  		{iterator _L = end();
                  		for (iterator _F = begin(); _F != _L; )
                  			if (_Pr(*_F))
                ***************
                *** 297,304 ****
                  					erase(_M);
                  				else
                  					_F = _M; }
                ! //	typedef not_equal_to<_Ty> _Pr2;		// Fixed 2000 06 27 JMG
                ! 	template<class _Pr2> void unique(_Pr2 _Pr)		// Fixed 2000 06 27 JMG
                  		{iterator _F = begin(), _L = end();
                  		if (_F != _L)
                  			for (iterator _M = _F; ++_M != _L; _M = _F)
                --- 297,304 ----
                  					erase(_M);
                  				else
                  					_F = _M; }
                ! 	typedef not_equal_to<_Ty> _Pr2;
                ! 	void unique(_Pr2 _Pr)
                  		{iterator _F = begin(), _L = end();
                  		if (_F != _L)
                  			for (iterator _M = _F; ++_M != _L; _M = _F)
                ***************
                *** 321,328 ****
                  				_Splice(_L1, _X, _F2, _L2);
                  			_Size += _X._Size;
                  			_X._Size = 0; }}
                ! //	typedef greater<_Ty> _Pr3;		// Fixed 2000 06 27 JMG
                ! 	template<class _Pr3> void merge(_Myt& _X, _Pr3 _Pr)		// Fixed 2000 06 27 JMG
                  		{if (&_X
                
                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups