オンラインでいくつかのチュートリアルを調べました。私は自分で試してみましたが、問題は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 を使用した場合に何かを出力します。しかし、そうではありません。問題がどこにあるのかわかりません。ここで何が問題で、どうすれば修正できますか?