我在網上查了幾個教程。我已經嘗試過,但問題是std::sort()
不執行任何操作!這是我的整個代碼:試圖對自定義對象使用std :: sort()
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
#ifndef _card_h
#define _card_h
enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
};
enum Rank {
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
};
class Card {
public:
Card();
Card(Rank, Suit);
~Card();
Rank GetRank();
Suit GetSuit();
string ToString();
private:
Rank rank;
Suit suit;
};
#endif
Card::Card() {
}
Card::Card(Rank rank, Suit suit) {
this->rank = rank;
this->suit = suit;
}
Card::~Card() {}
Rank Card::GetRank() {
return rank;
}
Suit Card::GetSuit() {
return suit;
}
string Card::ToString() {
string cardName = "";
switch (rank) {
case TWO : cardName += "2"; break;
case THREE : cardName += "3"; break;
case FOUR : cardName += "4"; break;
case FIVE : cardName += "5"; break;
case SIX : cardName += "6"; break;
case SEVEN : cardName += "7"; break;
case EIGHT : cardName += "8"; break;
case NINE : cardName += "9"; break;
case TEN : cardName += "T"; break;
case JACK : cardName += "J"; break;
case QUEEN : cardName += "Q"; break;
case KING : cardName += "K"; break;
case ACE : cardName += "A"; break;
}
switch (suit) {
case CLUBS : cardName += "C"; break;
case DIAMONDS : cardName += "D"; break;
case HEARTS : cardName += "H"; break;
case SPADES : cardName += "S"; break;
}
return cardName;
}
#ifndef _cardcomparer_h
#define _cardcomparer_h
class CardComparer {
public:
bool operator() (Card*, Card*);
private:
Card* firstCard;
Card* secondCard;
};
#endif
bool CardComparer::operator() (Card* firstCard, Card* secondCard) {
this->firstCard = firstCard;
this->secondCard = secondCard;
cout << "in Cardcompare! " << endl;
if (firstCard->GetRank() == secondCard->GetRank()) {
return firstCard->GetSuit() > secondCard->GetSuit();
}
else {
return firstCard->GetRank() > secondCard->GetRank();
}
}
#ifndef _hand_h
#define _hand_h
const int CARDS_IN_HAND = 5;
class Hand {
public:
Hand(int);
~Hand();
void AddCard(Card*);
string ToString();
private:
int cardCount;
int playerID;
vector<Card*> cards;
};
#endif
Hand::Hand(int playerID) {
this->playerID = playerID;
cards.reserve(CARDS_IN_HAND);
cardCount = 0;
}
Hand::~Hand() {
cards.clear();
}
void Hand::AddCard(Card* newCard) {
cards[cardCount] = newCard;
cardCount++;
sort(cards.begin(), cards.end(),CardComparer());
}
string Hand::ToString() {
stringstream playerCards;
playerCards << "Player " << this->playerID << " -";
for (int i = 0; i < cardCount; i++){
playerCards << " " << cards[i]->ToString();
}
return playerCards.str();
}
int main() {
vector<Hand*> hands;
hands.reserve(1);
hands[0] = new Hand(0);
hands[0]->AddCard(new Card((Rank)4, (Suit)1));
hands[0]->AddCard(new Card((Rank)8, (Suit)2));
hands[0]->AddCard(new Card((Rank)5, (Suit)1));
hands[0]->AddCard(new Card((Rank)2, (Suit)0));
hands[0]->AddCard(new Card((Rank)7, (Suit)3));
cout << hands[0]->ToString() << endl;
return 0;
}
問題是行133(sort(cards.begin(), cards.end(),CardComparer());
)不執行。無論我評論它還是結果都不會改變,我甚至在93(cout << "in Cardcompare! " << endl;
)上添加了一行,如果它使用bool,它會打印出一些內容。但事實並非如此。我找不到問題所在。這裏有什麼問題,我該如何解決它?
爲什麼在cardComparer中存儲firstCard和secondCard?你看起來並不需要它。 – Nobody 2013-04-24 06:27:14
如果不在每個地方都使用指針和'new',你也會好得多。在現實生活中,一隻手是一張牌的「矢量」,而不是一組指向牌的指針。這在程序中效果更好,例如'Hand.push_back(Card(ACE,SPADES));'。在現實生活中,我也拿起所有的卡片並在最後進行分類,我不會爲每張卡片採取行動。該計劃也可以對此進行建模。 – 2013-04-24 06:39:30