Trouble using the STL sort() algorithm with the vector container

__fletcher__ used Ask the Experts™
A quick explanation of what I'm doing might help to answer this question.  I have a Card class, and a CardPile class.  The CardPile class has a vector of Card*'s, and I'm trying to write a sort method to sort the pile(in ascending order).  My Card class has the operator<,operator>,operator<=,operator>=, and operator== methods defined.  My problem is that the vector holds pointers, and I don't want it to compare the pointers, but the actual cards themselves.  Is there a way to somehow get the sort method to dereference the iterators that it takes and use my operator methods on the cards instead of the pointers??
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
template <>
struct std::greater<Card*>
   bool operator()(const Card* lhs, const Card* rhs) const
       return lhs->val < rhs->val;

int main(int argc, char* argv[])
   std::list<Card*> templist;


hope this helps

std::sort(v.begin(), v.end(), std::greater<Card*>());


That's great, and I think I get the idea, but there are still a few things about that I don't understand(I'm not comfortable with template classes yet).  Can I define that template class and struct in my CardPile header file?
And I've never seen the std::... stuff...what's that for?
I mean, I know what it means, that the following is a definition for the std class...but what's it for?

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

>> class and struct in my CardPile header file?

yes you can, though it might be a good idea to include it only where it is needed.

>> And I've never seen the std::... stuff...what's that for?

the std that you see is the namespace, all the standard STL classes and templates reside in the namspace std. This avoids having name conflicts with other classes in the global namespace.

>> template <>
struct std::greater<Card*>

this is specialization of std::greater predicate for Card* type. specizlization is required when you want to handle certain cases specialy (as the name indicates too).

By specializing a template we tell the compiler to use this version of the template instantiation whenever it sees something like std::greater<Card*>.

wheever you include an STL header like say

#include <vector>


you can refer to any type by explicitly qualifying it by the std namespace. or you can bring all the types into the global namespace like

using namespace std;

from now on you can use things like say


hope this helped ...


Thank you so much, I think that will work.  I'm going to give it a try later on and let you know if it does what I need it to do.  Thanks again!!


ok...I tried it, and it gave me the following error.  Am I doing something wrong?

'greater' is not a template
explicit specialization ofnon-template '{anonymous struct}'
anonymous class type not used to declare any objects
Error 1


nevermind, I'm just and idiot and forgot to include <functional>.  Thanks for your help ambience!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial