在类中使用比较函数的问题sort() [英] Problem using a compare function inside of a class for sort()
问题描述
如果你看看我的功能CardCompare里面的类...它不工作!但是,如果我改用使用在Hand.cpp中注释掉的函数,它工作得很好。为什么是这样的?
此外,我想知道如果保持CardCompare函数在我的手类比保持在Card类中更有意义,如果可能的话。 / p>
Hand.h
#ifndef HAND_H
#define HAND_H
#include< vector>
#includeCard.h
class Hand {
private:
std :: vector< Card>手;
int total;
void CalculateTotal();
bool CardCompare(Card i,Card j){return(i.RankInt()< j.RankInt()); } //不工作! :O
public:
Hand(){
total = 0;
}
std :: vector< Card> GetHand()const {return hand;};
void printHand();
void AddToHand(Card c);
};
#endif
Hand.cpp
#includeHand.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;
}
/ *如果我把它放在这里,它的工作完美。
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;
西装;
static const char * ranknames [];
static const char * suitnames [];
static const int rankints [];
};
#endif
Card.cpp
#include< iostream>
#includeCard.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>甲板;
for(int i = 0; i <10; i ++){
Deck.push_back(Card(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;
}
} * /
你试图传递一个指向成员函数的指针,所以sort不能使用它,因为它没有 this
指针。在你的case你可以只是更改函数为 static
:
static bool CardCompare(Card i,Card j){return(i.RankInt()< j.RankInt()); }
如果你确实需要它在未来是一个非静态成员函数, code> boost :: bind 或 std :: bind
(对于C ++ 0x编译器):
std :: sort(hand.begin(),hand.end(),bind(& Hand :: CardCompare,this,_1,_2)) ;
If you take a look at my function CardCompare inside of the class... It doesn't work! But, if I instead use the function where it is commented out in Hand.cpp, it works perfectly fine. Why is this?
Also, I am wondering if keeping the CardCompare function in my hand class makes less sense than keeping it in the Card class, if that is possible.
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;
}
}*/
You are trying to pass a pointer to a member function, so sort can't use it because it doesn't have this
pointer. In your case you can just change the function to be static
:
static bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }
If you do need it be a non-static member function in the future, bind it with boost::bind
or std::bind
(for C++0x compiler):
std::sort(hand.begin(),hand.end(),bind(&Hand::CardCompare, this, _1, _2));
这篇关于在类中使用比较函数的问题sort()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!