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. Doubts with sorting stl vector...

Doubts with sorting stl vector...

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsalgorithmshelpquestion
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.
  • C Offline
    C Offline
    Cloaca
    wrote on last edited by
    #1

    Hi everyone, I have a class like this:

    // Header file
    class CFoo
    {
    public:
    CFoo();
    ~CFoo();
    BOOL SortByName();
    // Other functions declared here...

    protected:
    std::vector<CRec*> m_Recs; // CRec is a class with CStrings, etc.

    private:
    struct NameComparer
    {
    bool operator()(const CRec* A,const CRec* B)
    {
    return ((A->m_strName) < (B->m_strName));
    }
    };
    };

    // CPP file
    // Other functions are defined here...
    BOOL CFoo::SortByName()
    {
    std::stable_sort(m_Recs.begin(), m_Recs.end(), NameComparer());

    return TRUE;
    

    }

    Now, basically, I am trying to properly write and use a predicate function (I think that's what they're called; please correct me if I'm wrong) in the context of calling std::stable_sort (and just std::sort for that matter). There are many helpful examples around, but they don't seem to cover dealing with a non-trivial class (or if they do I haven't 'gotten it' yet). In my case, I want to be able to sort the vector of CRec's according to several different CRec member variables (different CStrings, ints, doubles, etc.). My questions are: 1) Did I use proper coding techniques to solve the problem as I have shown here? 2) Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write (with the comparisons applied to the std::vector m_Recs and based on the CRec members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? Thank you all again for reading and giving any advice, Best, :) Eric

    D V J 3 Replies Last reply
    0
    • C Cloaca

      Hi everyone, I have a class like this:

      // Header file
      class CFoo
      {
      public:
      CFoo();
      ~CFoo();
      BOOL SortByName();
      // Other functions declared here...

      protected:
      std::vector<CRec*> m_Recs; // CRec is a class with CStrings, etc.

      private:
      struct NameComparer
      {
      bool operator()(const CRec* A,const CRec* B)
      {
      return ((A->m_strName) < (B->m_strName));
      }
      };
      };

      // CPP file
      // Other functions are defined here...
      BOOL CFoo::SortByName()
      {
      std::stable_sort(m_Recs.begin(), m_Recs.end(), NameComparer());

      return TRUE;
      

      }

      Now, basically, I am trying to properly write and use a predicate function (I think that's what they're called; please correct me if I'm wrong) in the context of calling std::stable_sort (and just std::sort for that matter). There are many helpful examples around, but they don't seem to cover dealing with a non-trivial class (or if they do I haven't 'gotten it' yet). In my case, I want to be able to sort the vector of CRec's according to several different CRec member variables (different CStrings, ints, doubles, etc.). My questions are: 1) Did I use proper coding techniques to solve the problem as I have shown here? 2) Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write (with the comparisons applied to the std::vector m_Recs and based on the CRec members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? Thank you all again for reading and giving any advice, Best, :) Eric

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      It's usually best to wait a few days before posting the same question again. And, surprisingly, an answer may not be know by the current CP members!


      A rich person is not the one who has the most, but the one that needs the least.

      C 1 Reply Last reply
      0
      • C Cloaca

        Hi everyone, I have a class like this:

        // Header file
        class CFoo
        {
        public:
        CFoo();
        ~CFoo();
        BOOL SortByName();
        // Other functions declared here...

        protected:
        std::vector<CRec*> m_Recs; // CRec is a class with CStrings, etc.

        private:
        struct NameComparer
        {
        bool operator()(const CRec* A,const CRec* B)
        {
        return ((A->m_strName) < (B->m_strName));
        }
        };
        };

        // CPP file
        // Other functions are defined here...
        BOOL CFoo::SortByName()
        {
        std::stable_sort(m_Recs.begin(), m_Recs.end(), NameComparer());

        return TRUE;
        

        }

        Now, basically, I am trying to properly write and use a predicate function (I think that's what they're called; please correct me if I'm wrong) in the context of calling std::stable_sort (and just std::sort for that matter). There are many helpful examples around, but they don't seem to cover dealing with a non-trivial class (or if they do I haven't 'gotten it' yet). In my case, I want to be able to sort the vector of CRec's according to several different CRec member variables (different CStrings, ints, doubles, etc.). My questions are: 1) Did I use proper coding techniques to solve the problem as I have shown here? 2) Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write (with the comparisons applied to the std::vector m_Recs and based on the CRec members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? Thank you all again for reading and giving any advice, Best, :) Eric

        V Offline
        V Offline
        valikac
        wrote on last edited by
        #3

        In sum, your solution is okay. In general, check out these books for in-depth explanation on STL. The C++ Standard Library : A Tutorial and Reference by Nicolai M. Josuttis Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library by Scott Meyers Kuphryn

        C 1 Reply Last reply
        0
        • D David Crow

          It's usually best to wait a few days before posting the same question again. And, surprisingly, an answer may not be know by the current CP members!


          A rich person is not the one who has the most, but the one that needs the least.

          C Offline
          C Offline
          Cloaca
          wrote on last edited by
          #4

          Hi David, I know. And I knew that I risked being rude, but I figured that since it was late when I left the post it might not get seen by many, so I took the chance. Anyhow, I take your point and I appreciate your feedback. Best, Eric

          D 1 Reply Last reply
          0
          • V valikac

            In sum, your solution is okay. In general, check out these books for in-depth explanation on STL. The C++ Standard Library : A Tutorial and Reference by Nicolai M. Josuttis Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library by Scott Meyers Kuphryn

            C Offline
            C Offline
            Cloaca
            wrote on last edited by
            #5

            Hi Kuphryn, I will grab these two references that you noted. And thanks for the comment on the way I'm doing it now, I appreciate your input! Thanks very much, Eric

            1 Reply Last reply
            0
            • C Cloaca

              Hi David, I know. And I knew that I risked being rude, but I figured that since it was late when I left the post it might not get seen by many, so I took the chance. Anyhow, I take your point and I appreciate your feedback. Best, Eric

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Cloaca wrote: And I knew that I risked being rude... It was being rude per se, it's just that with timezone differences and the like, it may take two full days before the post has been digested by everyone.


              A rich person is not the one who has the most, but the one that needs the least.

              C 1 Reply Last reply
              0
              • C Cloaca

                Hi everyone, I have a class like this:

                // Header file
                class CFoo
                {
                public:
                CFoo();
                ~CFoo();
                BOOL SortByName();
                // Other functions declared here...

                protected:
                std::vector<CRec*> m_Recs; // CRec is a class with CStrings, etc.

                private:
                struct NameComparer
                {
                bool operator()(const CRec* A,const CRec* B)
                {
                return ((A->m_strName) < (B->m_strName));
                }
                };
                };

                // CPP file
                // Other functions are defined here...
                BOOL CFoo::SortByName()
                {
                std::stable_sort(m_Recs.begin(), m_Recs.end(), NameComparer());

                return TRUE;
                

                }

                Now, basically, I am trying to properly write and use a predicate function (I think that's what they're called; please correct me if I'm wrong) in the context of calling std::stable_sort (and just std::sort for that matter). There are many helpful examples around, but they don't seem to cover dealing with a non-trivial class (or if they do I haven't 'gotten it' yet). In my case, I want to be able to sort the vector of CRec's according to several different CRec member variables (different CStrings, ints, doubles, etc.). My questions are: 1) Did I use proper coding techniques to solve the problem as I have shown here? 2) Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write (with the comparisons applied to the std::vector m_Recs and based on the CRec members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? Thank you all again for reading and giving any advice, Best, :) Eric

                J Offline
                J Offline
                jhwurmbach
                wrote on last edited by
                #7

                Cloaca wrote: Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write [...]? Yes. AFAIK every comparison need its own comparer (like you have with the NameComparer) Cloaca wrote: 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? struct does mean the same as class, but everything is public by default. As you only want a class with only one public operator, writing struct instead of class does save you a whopping 6 keystrokes (7 for 'public' plus the ':', minus one because struct is longer than class. I think that is the only reason.


                "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                C 1 Reply Last reply
                0
                • D David Crow

                  Cloaca wrote: And I knew that I risked being rude... It was being rude per se, it's just that with timezone differences and the like, it may take two full days before the post has been digested by everyone.


                  A rich person is not the one who has the most, but the one that needs the least.

                  C Offline
                  C Offline
                  Cloaca
                  wrote on last edited by
                  #8

                  You are right David. That's a good point also. I should male it a policy not to repost for a week or so I guess. Thanks again, Eric :)

                  1 Reply Last reply
                  0
                  • J jhwurmbach

                    Cloaca wrote: Is it the case that I need to add additional structs under the private section of CFoo for each comparison CFoo member function I want to write [...]? Yes. AFAIK every comparison need its own comparer (like you have with the NameComparer) Cloaca wrote: 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate to operator(), etc.? struct does mean the same as class, but everything is public by default. As you only want a class with only one public operator, writing struct instead of class does save you a whopping 6 keystrokes (7 for 'public' plus the ':', minus one because struct is longer than class. I think that is the only reason.


                    "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

                    C Offline
                    C Offline
                    Cloaca
                    wrote on last edited by
                    #9

                    Hi jhwurmbach, Thanks for your reply. That makes sense, but it then makes me wonder why I need a class (or struct) at all to hold the comparison function. Why is it that I just can't declare the comparison function (just as a 'simple' member of CFoo) and pass it to the stable_sort() call? I have been reading about operator() and functors, but I am not putting it all together yet, I guess. Does it have someting to do with the fact that functions passed to the algorithms can only have one parameter? I appreciate your help, thanks again! :) Eric

                    A 1 Reply Last reply
                    0
                    • C Cloaca

                      Hi jhwurmbach, Thanks for your reply. That makes sense, but it then makes me wonder why I need a class (or struct) at all to hold the comparison function. Why is it that I just can't declare the comparison function (just as a 'simple' member of CFoo) and pass it to the stable_sort() call? I have been reading about operator() and functors, but I am not putting it all together yet, I guess. Does it have someting to do with the fact that functions passed to the algorithms can only have one parameter? I appreciate your help, thanks again! :) Eric

                      A Offline
                      A Offline
                      antlers
                      wrote on last edited by
                      #10

                      stds::sort is a template function. That means the arguments for the function determines what kind of code the compiler generates for the function (as opposed to a regular function, where the compiler always generates the same code). A template argument must be either a type or a constant--the compiler must be able to determine what the argument is at compile time, so it knows what code to generate. For the sort function, the template argument might have been a functor type or it might have been a constant that resolves to a function pointer. If you think about it, you can see how making the argument a type gives much more flexibility to the user of the template function, so that is the choice that the template designer made.

                      C 1 Reply Last reply
                      0
                      • A antlers

                        stds::sort is a template function. That means the arguments for the function determines what kind of code the compiler generates for the function (as opposed to a regular function, where the compiler always generates the same code). A template argument must be either a type or a constant--the compiler must be able to determine what the argument is at compile time, so it knows what code to generate. For the sort function, the template argument might have been a functor type or it might have been a constant that resolves to a function pointer. If you think about it, you can see how making the argument a type gives much more flexibility to the user of the template function, so that is the choice that the template designer made.

                        C Offline
                        C Offline
                        Cloaca
                        wrote on last edited by
                        #11

                        Hi antlers, Ahh. I think that puts it together for me. A function is of course not a type. But by using the 'struct method', you kind-of wrap the function in a type. And so it is acceptable to the algorithm at compile time. The syntax of operator() is the way a wrapped function can be exposed to the algorithm for use in, for example, a sort call as a comparison. And this minimal extra work is the small price to pay for a very flexible system of getting whatever might be needed into the templated algorithms. I see. Thanks very much for your help, I appreciate it! :) Best, Eric

                        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