使用类内部的比较函数进行sort()的问题

时间:2010-11-26 20:46:13

标签: c++

如果你看看班级里面我的功能CardCompare ......它不起作用!但是,如果我改为使用Hand.cpp中注释掉的函数,它的工作原理非常好。这是为什么?

另外,我想知道如果可能的话,将CardCompare函数保留在我的手类中比将它保留在Card类中更有意义。

Hand.h

#ifndef HAND_H
#define HAND_H

#include <vector>
#include "Card.h"

class Hand {
    private:
        std::vector<Card> hand;
        int total;
        void CalculateTotal();
        bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }//Does not work! :O
    public:
        Hand() {
            total = 0;
        }
        std::vector<Card> GetHand() const{ return hand;};
        void PrintHand();
        void AddToHand(Card c);

};



#endif

Hand.cpp

#include "Hand.h"
#include <iostream>
#include <algorithm>

void Hand::CalculateTotal() {
    for (int i = 0; i < hand.size(); i++) {
            std::cout << hand[i].ToString() << std::endl;
    }
}

void Hand::PrintHand() {
    for (int i = 0; i < hand.size(); i++) {
        std::cout << hand[i].ToString() << std::endl;
    }
    std::cout << std::endl;
}
/* If I place this right here, it works perfect.
bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }
*/
void Hand::AddToHand(Card c) {
    hand.push_back(c);
    std::sort(hand.begin(),hand.end(),CardCompare);
}

int main() {
    Hand h;
    h.PrintHand();
    h.AddToHand(Card (2, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (3, ( Card::Suit )3 ) );
    h.PrintHand();
    h.PrintHand();
    h.AddToHand(Card (1, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (13, ( Card::Suit )3 ) );
    h.PrintHand();

    std::cout<< std::endl << std::endl;

    std::cout << h.GetHand()[0].ToString();
}

Card.h

#ifndef CARD_H
#define CARD_H

#include <string>

class Card {
public:
    enum Suit {
        SUIT_HEART,
        SUIT_DIAMOND,
        SUIT_CLUB,
        SUIT_SPADE
    };
    Card(int r = 1, Suit s = SUIT_HEART) : rank(r), suit(s)
    {}
    int GetRank() const { return rank; };
    Suit GetSuit() const { return suit; };
    std::string ToString() const;
    std::string SuitString() const;
    std::string RankString() const;
    int RankInt() const;


private:
    int rank;
    Suit suit;
    static const char * ranknames[];
    static const char * suitnames[];
    static const int     rankints[];
};
#endif

Card.cpp

#include <iostream>
#include "Card.h"
//#include <vector> //gtfo

const char * Card::ranknames[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const char * Card::suitnames[] = { "Hearts", "Diamonds", "Clubs", "Spaces" };
const int    Card::rankints[]  = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,10 ,10, 10 };

std::string Card::ToString() const {
    std::string s = RankString();
    s.append(" of ");
    s.append(SuitString());
    return s;
}

std::string Card::SuitString() const {
    return suitnames[suit];
}

std::string Card::RankString() const {
    return ranknames[rank-1];
}

int Card::RankInt() const {
    return rankints[rank-1];
}
 /*
int main() {

    std::vector<Card> Deck;

    for (int i = 0; i < 10 ; i++) {
        Deck.push_back(Card(i+1,(Card::Suit)((i+1)%4)));
        std::cout << Deck[i].ToString() << std::endl;
    }

    std::cout << std::endl << std::endl;
    std::random_shuffle( Deck.begin(), Deck.end() );

    for (int i = 0; i < 10 ; i++) {
            std::cout << Deck[i].ToString() << std::endl;
    }
}*/

3 个答案:

答案 0 :(得分:9)

您正在尝试将指针传递给成员函数,因此sort不能使用它,因为它没有this指针。在您的情况下,您只需将功能更改为static

static bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }

如果将来确实需要它是非静态成员函数,请将其与boost::bindstd::bind绑定(对于C ++ 0x编译器):

std::sort(hand.begin(),hand.end(),bind(&Hand::CardCompare, this, _1, _2));

答案 1 :(得分:2)

如果要在sort()中使用,则CardCompare()不能是成员函数。你可以重载运算符&lt;在卡类中比较卡片。

在卡类中,类似于:

bool operator<(const Card& other) const {
    return RankInt() < other.RankInt();
}

答案 2 :(得分:1)

规范的解决方案是重载operator()。这会将您的类转换为仿函数,然后它将使用标准库算法开箱即用。

只需更改

bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }

bool operator()(Card i, Card j) {return ( i.RankInt() < j.RankInt() ); } 

然后你可以调用这样的排序:

std::sort(hand.begin(),hand.end(), Hand());

通常,您将比较运算符放在单独的类中,但