Using stable_sort with predicate function: Help please.
-
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 juststd::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 ofCRec
's according to several differentCRec
member variables (differentCString
s,int
s,double
s, 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 additionalstruct
s under theprivate
section ofCFoo
for each comparisonCFoo
member function I want to write (with the comparisons applied to thestd::vector m_Recs
and based on theCRec
members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate tooperator()
, etc.? Thank you all again for reading and giving any advice, Best, :) Eric -
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 juststd::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 ofCRec
's according to several differentCRec
member variables (differentCString
s,int
s,double
s, 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 additionalstruct
s under theprivate
section ofCFoo
for each comparisonCFoo
member function I want to write (with the comparisons applied to thestd::vector m_Recs
and based on theCRec
members themselves actually)? 3) Why do I need to wrap the comparison functions in a struct? How does that fact relate tooperator()
, etc.? Thank you all again for reading and giving any advice, Best, :) Eric