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. Using stable_sort with predicate function: Help please.

Using stable_sort with predicate function: Help please.

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++graphicsquestion
2 Posts 1 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

    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

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

      Any ideas? :((

      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