I am trying to sort a vector that is a protected member of a templated class into a Descending order based on the second member of the contained std::pair objects. The vector has the following declaration:
vector< pair <const T, int> > mode; //holds pairs to define the mode(s) of the dataSet
I'm aware of the std::map container, and I had considered it. The problem is the strict weak ordering that it requires is not appropriate for the problem domain as the ordering is based on the first member of the pairs and I need the values sorted based on the second member of the pairs. I would reverse the roles, but the second member isn't unique across the set, so I can't.
I'm trying to provide the sort function with a comparison function declared and implemented thus:
bool Descending(const pair<const T, int> &, const pair<const T, int> &) const;
template <typename T> bool Stats<T>::Descending(const pair<const T, int> &lhs,
const pair<const T, int> &rhs) const {
return (lhs.second > rhs.second);
}
Based on the reference material(s) @ cplusplus.com,, I'm calling the 3-argument version of sort correctly. I have tried it 4 different ways (implicitly and explicitly):
sort(mode.begin(), mode.end(), Descending); //implicit call 1
std::sort(mode.begin(), mode.end(), Descending); //implicit call 2
sort< vector <pair<const T, int> >::const_iterator, bool>(mode.begin(), mode.end(), Descending); //explicit call 1
std::sort< vector < pair< const T, int> >::const_iterator, bool>(mode.begin(), mode.end(), Descending); //explicit call 2
In all instances, I get this error:
stats.cpp(87) : error C3867: 'Statistics::Stats<T>::Descending': function call missing argument list; use '&Statistics::Stats<T>::Descending' to create a pointer to member
If I try to use the reference operator as it suggests, it tells me that it's illegal. If I change the reference to &Stats<T>::Descending
, I get an error in the xutility library file related to the pair and the assignment operator that I wasn't getting before.
Additionally, the implicit versions get this:
stats.cpp(88) : error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
The sort takes place inside a member function of the template class, so I should have access to the data members:
template <typename T> bool Stats<T>::UpdateMode(const T &newDatum) {
using std::sort;
} //end Stats<T>::UpdateMode()
I have tried moving the comparison function from protected to public, but it didn't make a difference. There are 2 places where I do this, they are both identical calls and have identical member access, but the second one refuses to work correctly. What have I got wrong?
I'm trying to avoid overloading operator< for the 2-argument version. I need the values sorted in Descending order instead of Ascending order so I would have to overload it "backwards" from how it should function.