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. std::sort( Ran first, Ran last, Cmp cmp)

std::sort( Ran first, Ran last, Cmp cmp)

Scheduled Pinned Locked Moved C / C++ / MFC
11 Posts 5 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.
  • L Offline
    L Offline
    Laing James
    wrote on last edited by
    #1

    Hi. Can anyone please advise on how I can specify a class member function as the 'Cmp' function in the above call. I would like to be able to call the sort() function from within a class member function and to be able to specify another class member function( of the same class and instantiation ). I do not want to declare a struct that defines a comparison operator etc.. I would like to be able to keep the whole process private to a class that defines a 'Cmp' type function that I can provide as an argument. Thanks in advance. James.

    M M P 3 Replies Last reply
    0
    • L Laing James

      Hi. Can anyone please advise on how I can specify a class member function as the 'Cmp' function in the above call. I would like to be able to call the sort() function from within a class member function and to be able to specify another class member function( of the same class and instantiation ). I do not want to declare a struct that defines a comparison operator etc.. I would like to be able to keep the whole process private to a class that defines a 'Cmp' type function that I can provide as an argument. Thanks in advance. James.

      M Offline
      M Offline
      Milton Karimbekallil
      wrote on last edited by
      #2

      hi Yes it is possible, provided the user-defined binary predicate function shud b static. Otherwise u know, while linking a class, every member function will get an _this pointer automatically. This changes the function signature of a predicate function and will show error while compilation. If it is static the parameter list will remain unchanged. c the sample class. rgds..mil10 using namespace std; class TstClass { public: vector _data; void loaddata() { _data.push_back(20); _data.push_back(10); _data.push_back(30); } static bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } void displaysorted() { sort(v.begin(),v.end(),UDgreater); vector::iterator it; for( it=v.begin(); it!=v.end(); it++ ) cout<<(*it); } };

      M 1 Reply Last reply
      0
      • M Milton Karimbekallil

        hi Yes it is possible, provided the user-defined binary predicate function shud b static. Otherwise u know, while linking a class, every member function will get an _this pointer automatically. This changes the function signature of a predicate function and will show error while compilation. If it is static the parameter list will remain unchanged. c the sample class. rgds..mil10 using namespace std; class TstClass { public: vector _data; void loaddata() { _data.push_back(20); _data.push_back(10); _data.push_back(30); } static bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } void displaysorted() { sort(v.begin(),v.end(),UDgreater); vector::iterator it; for( it=v.begin(); it!=v.end(); it++ ) cout<<(*it); } };

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3

        Mil10 wrote: Yes it is possible, provided the user-defined binary predicate function shud b static. Otherwise u know, while linking a class, every member function will get an _this pointer automatically. This changes the function signature of a predicate function and will show error while compilation. From Section 11.5 in "The C++ Programming Language, 3rd": An ordinary member function declaration specifies three logically distinct things: (1) The function can access the private part of the class declaration, and (2) the function is in the scope of the class, and (3) the function must be invoked on an object (has a this pointer). By declaraing a member function static ($10.2.4), we can give it the first two properties only. By declaring a function a friend, we can give it the first property only. Maxwell Chen

        L M 2 Replies Last reply
        0
        • M Maxwell Chen

          Mil10 wrote: Yes it is possible, provided the user-defined binary predicate function shud b static. Otherwise u know, while linking a class, every member function will get an _this pointer automatically. This changes the function signature of a predicate function and will show error while compilation. From Section 11.5 in "The C++ Programming Language, 3rd": An ordinary member function declaration specifies three logically distinct things: (1) The function can access the private part of the class declaration, and (2) the function is in the scope of the class, and (3) the function must be invoked on an object (has a this pointer). By declaraing a member function static ($10.2.4), we can give it the first two properties only. By declaring a function a friend, we can give it the first property only. Maxwell Chen

          L Offline
          L Offline
          Laing James
          wrote on last edited by
          #4

          Thanks very much for the help. James.

          1 Reply Last reply
          0
          • L Laing James

            Hi. Can anyone please advise on how I can specify a class member function as the 'Cmp' function in the above call. I would like to be able to call the sort() function from within a class member function and to be able to specify another class member function( of the same class and instantiation ). I do not want to declare a struct that defines a comparison operator etc.. I would like to be able to keep the whole process private to a class that defines a 'Cmp' type function that I can provide as an argument. Thanks in advance. James.

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            I think you want the mem_fun adapter function, which returns an appropriate functor object (typically a mem_fun1_t object). Stability. What an interesting concept. -- Chris Maunder

            1 Reply Last reply
            0
            • L Laing James

              Hi. Can anyone please advise on how I can specify a class member function as the 'Cmp' function in the above call. I would like to be able to call the sort() function from within a class member function and to be able to specify another class member function( of the same class and instantiation ). I do not want to declare a struct that defines a comparison operator etc.. I would like to be able to keep the whole process private to a class that defines a 'Cmp' type function that I can provide as an argument. Thanks in advance. James.

              P Offline
              P Offline
              Paul Ranson
              wrote on last edited by
              #6

              Do you mean something like,

              class Sort1
              {
              private :
              std::vector<int> v_ ;

              static bool Cmp ( const int l, const int r )
              {
                  return l > r  ;
              }
              

              public :
              Sort1 ()
              {
              v_.resize ( 10 ) ;
              for ( int i = 0; i < 10; ++i )
              {
              v_ [ i ] = i ;
              }
              }
              void DoSomething ()
              {
              std::sort ( v_.begin (), v_.end (), Cmp ) ;
              }
              void PrintV_ ()
              {
              for ( unsigned i = 0; i < v_.size (); ++i )
              {
              std::cout << v_ [ i ] << " " ;
              }
              std::cout << "\n" ;
              }
              } ;

              int main()
              {
              Sort1 s ;
              s.PrintV_ () ;
              s.DoSomething () ;
              s.PrintV_ () ;

              return 0;
              

              }

              Or are you trying to use a member of the sortable class as the comparison function?

              class Sortable
              {
              private :
              int val_ ;
              public :
              Sortable ()
              {
              }
              static bool Cmp ( const Sortable& l, const Sortable& r )
              {
              return l.val_ > r.val_ ;
              }
              } ;
              ...
              std::vector vs ;
              ...
              std::sort ( vs.begin (), vs.end (), Sortable::Cmp ) ;

              Paul

              L 1 Reply Last reply
              0
              • P Paul Ranson

                Do you mean something like,

                class Sort1
                {
                private :
                std::vector<int> v_ ;

                static bool Cmp ( const int l, const int r )
                {
                    return l > r  ;
                }
                

                public :
                Sort1 ()
                {
                v_.resize ( 10 ) ;
                for ( int i = 0; i < 10; ++i )
                {
                v_ [ i ] = i ;
                }
                }
                void DoSomething ()
                {
                std::sort ( v_.begin (), v_.end (), Cmp ) ;
                }
                void PrintV_ ()
                {
                for ( unsigned i = 0; i < v_.size (); ++i )
                {
                std::cout << v_ [ i ] << " " ;
                }
                std::cout << "\n" ;
                }
                } ;

                int main()
                {
                Sort1 s ;
                s.PrintV_ () ;
                s.DoSomething () ;
                s.PrintV_ () ;

                return 0;
                

                }

                Or are you trying to use a member of the sortable class as the comparison function?

                class Sortable
                {
                private :
                int val_ ;
                public :
                Sortable ()
                {
                }
                static bool Cmp ( const Sortable& l, const Sortable& r )
                {
                return l.val_ > r.val_ ;
                }
                } ;
                ...
                std::vector vs ;
                ...
                std::sort ( vs.begin (), vs.end (), Sortable::Cmp ) ;

                Paul

                L Offline
                L Offline
                Laing James
                wrote on last edited by
                #7

                Hi. I have looked at all the updates to this thread and I am a little unsure as to how to proceed. I appreciate all the advice and the different types of approach. However, if I may describe the problem with some additional constraints. I cannot use the static member function approach as I need to be able to access a set of compound sort ranges defined in a vector table declared privately within the class. I therefore need a mechanism that would allow me to specify a non-static class member function as the comparison function on the sort call. Any more advice would be most welcome. James.

                L P 2 Replies Last reply
                0
                • L Laing James

                  Hi. I have looked at all the updates to this thread and I am a little unsure as to how to proceed. I appreciate all the advice and the different types of approach. However, if I may describe the problem with some additional constraints. I cannot use the static member function approach as I need to be able to access a set of compound sort ranges defined in a vector table declared privately within the class. I therefore need a mechanism that would allow me to specify a non-static class member function as the comparison function on the sort call. Any more advice would be most welcome. James.

                  L Offline
                  L Offline
                  Laing James
                  wrote on last edited by
                  #8

                  Hi. If no one has a solution to this particular situation, then I might be better served by simply writing my own sort routine. That way I can provide a comparison function with the operands and access that I require. I have written numerous sort routines before, especially in the days before C++ and the STL ( as you might have guessed, I am quite new to C++). I was hoping, prehaps naively, that the STL could solve all my algorithmic problems elegantly but maybe that is just not the case. Thanks for the contributions. James.

                  1 Reply Last reply
                  0
                  • L Laing James

                    Hi. I have looked at all the updates to this thread and I am a little unsure as to how to proceed. I appreciate all the advice and the different types of approach. However, if I may describe the problem with some additional constraints. I cannot use the static member function approach as I need to be able to access a set of compound sort ranges defined in a vector table declared privately within the class. I therefore need a mechanism that would allow me to specify a non-static class member function as the comparison function on the sort call. Any more advice would be most welcome. James.

                    P Offline
                    P Offline
                    Paul Ranson
                    wrote on last edited by
                    #9

                    You may consider this too ugly...

                    template <typename C, typename S> struct MemFunCmp_t
                    {
                    bool (C::*pmf_ )( const S&, const S& ) ;
                    C& rC_ ;

                    MemFunCmp\_t ( C& rC, bool (C::\*f)( const S&, const S& )) : rC\_ ( rC ), pmf\_ ( f )
                    {
                    }
                    bool operator () ( const S& l, const S& r )
                    {
                        return (rC\_.\*pmf\_) ( l, r ) ;
                    }
                    

                    } ;

                    template <typename C, typename S> MemFunCmp_t< C, S > MemFunCmp ( C& c, bool (C::*f)( const S&, const S&))
                    {
                    return MemFunCmp_t <C, S>( c, f ) ;
                    }

                    class Sort2
                    {
                    private :
                    std::vector<int> v_ ;

                    bool Cmp2 ( const int& l, const int& r )
                    {
                        return l > r ;
                    }
                    

                    public :
                    Sort2 ()
                    {
                    v_.resize ( 10 ) ;
                    for ( int i = 0; i < 10; ++i )
                    {
                    v_ [ i ] = i ;
                    }
                    }
                    void DoSomething ()
                    {
                    std::sort ( v_.begin (), v_.end (), MemFunCmp ( *this, Cmp2 )) ;
                    }
                    void PrintV_ ()
                    {
                    for ( unsigned i = 0; i < v_.size (); ++i )
                    {
                    std::cout << v_ [ i ] << " " ;
                    }
                    std::cout << "\n" ;
                    }
                    } ;

                    int main()
                    {
                    Sort2 s ;
                    s.PrintV_ () ;
                    s.DoSomething () ;
                    s.PrintV_ () ;

                    return 0;
                    

                    }

                    But I think it does what you're asking for. It builds and runs on VC7.1, I believe it's standard C++. YMMV. Paul

                    L 1 Reply Last reply
                    0
                    • M Maxwell Chen

                      Mil10 wrote: Yes it is possible, provided the user-defined binary predicate function shud b static. Otherwise u know, while linking a class, every member function will get an _this pointer automatically. This changes the function signature of a predicate function and will show error while compilation. From Section 11.5 in "The C++ Programming Language, 3rd": An ordinary member function declaration specifies three logically distinct things: (1) The function can access the private part of the class declaration, and (2) the function is in the scope of the class, and (3) the function must be invoked on an object (has a this pointer). By declaraing a member function static ($10.2.4), we can give it the first two properties only. By declaring a function a friend, we can give it the first property only. Maxwell Chen

                      M Offline
                      M Offline
                      Milton Karimbekallil
                      wrote on last edited by
                      #10

                      From Section 11.5 in "The C++ Programming Language, 3rd": An ordinary member function declaration specifies three logically distinct things: (1) The function can access the private part of the class declaration, and (2) the function is in the scope of the class, and (3) the function must be invoked on an object (has a this pointer). By declaraing a member function static ($10.2.4), we can give it the first two properties only. By declaring a function a friend, we can give it the first property only. very nice explanation - thanx Chen. rgds...mil10

                      1 Reply Last reply
                      0
                      • P Paul Ranson

                        You may consider this too ugly...

                        template <typename C, typename S> struct MemFunCmp_t
                        {
                        bool (C::*pmf_ )( const S&, const S& ) ;
                        C& rC_ ;

                        MemFunCmp\_t ( C& rC, bool (C::\*f)( const S&, const S& )) : rC\_ ( rC ), pmf\_ ( f )
                        {
                        }
                        bool operator () ( const S& l, const S& r )
                        {
                            return (rC\_.\*pmf\_) ( l, r ) ;
                        }
                        

                        } ;

                        template <typename C, typename S> MemFunCmp_t< C, S > MemFunCmp ( C& c, bool (C::*f)( const S&, const S&))
                        {
                        return MemFunCmp_t <C, S>( c, f ) ;
                        }

                        class Sort2
                        {
                        private :
                        std::vector<int> v_ ;

                        bool Cmp2 ( const int& l, const int& r )
                        {
                            return l > r ;
                        }
                        

                        public :
                        Sort2 ()
                        {
                        v_.resize ( 10 ) ;
                        for ( int i = 0; i < 10; ++i )
                        {
                        v_ [ i ] = i ;
                        }
                        }
                        void DoSomething ()
                        {
                        std::sort ( v_.begin (), v_.end (), MemFunCmp ( *this, Cmp2 )) ;
                        }
                        void PrintV_ ()
                        {
                        for ( unsigned i = 0; i < v_.size (); ++i )
                        {
                        std::cout << v_ [ i ] << " " ;
                        }
                        std::cout << "\n" ;
                        }
                        } ;

                        int main()
                        {
                        Sort2 s ;
                        s.PrintV_ () ;
                        s.DoSomething () ;
                        s.PrintV_ () ;

                        return 0;
                        

                        }

                        But I think it does what you're asking for. It builds and runs on VC7.1, I believe it's standard C++. YMMV. Paul

                        L Offline
                        L Offline
                        Laing James
                        wrote on last edited by
                        #11

                        Hi. Thanks for the update. The 'work around' is not at all ugly but prehaps slightly convoluted. Nevertheless, I have incorporated it into my application and it works prefectly. I like your solution because I have learnt something new from it. Best Regards. James.

                        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