2013-04-24 46 views
0

我在網上查了幾個教程。我已經嘗試過,但問題是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,它會打印出一些內容。但事實並非如此。我找不到問題所在。這裏有什麼問題,我該如何解決它?

+0

爲什麼在cardComparer中存儲firstCard和secondCard?你看起來並不需要它。 – Nobody 2013-04-24 06:27:14

+0

如果不在每個地方都使用指針和'new',你也會好得多。在現實生活中,一隻手是一張牌的「矢量」,而不是一組指向牌的指針。這在程序中效果更好,例如'Hand.push_back(Card(ACE,SPADES));'。在現實生活中,我也拿起所有的卡片並在最後進行分類,我不會爲每張卡片採取行動。該計劃也可以對此進行建模。 – 2013-04-24 06:39:30

回答

3

的問題是,矢量確實不知道你想加入AddCard卡,或者更準確地說,你沒有真的添加這些卡,因爲你沒叫push_back但簡單地把指針某處內存。是的,由於致電reserve,矢量擁有該內存,但它認爲它是空的。因此begin()end()給出了相同的迭代器和排序正好0個元素。

查看關於vector和其他容器的章節。你不需要跟蹤卡的數量(cardCount),因爲如果使用正確的話,矢量對你來說是完美的。

+0

謝謝,解決了它! – 2013-04-24 06:31:56

+0

@ user1292095:還要注意'main()'中向量'hands'的相同問題。 – Nobody 2013-04-24 06:32:39

0

您的代碼有許多問題需要解決。其中最重要的是你正在泄漏大量的記憶。你聲明vector<Card*>並分配內存,但從不釋放它。您輸入數據的方式也不正確。保留函數實際上並不創建新元素,它只是爲它們保留內存。

這是一個清理版本,包括讓您的排序正確工作。希望通過學習它可以學習一些關於C++的知識。

#include <string> 
#include <iostream> 
#include <iomanip> 
#include <sstream> 
#include <algorithm> 
#include <vector> 
#include <memory> 
using namespace std; 

enum Suit { 
    CLUBS, DIAMONDS, HEARTS, SPADES 
}; 

enum Rank { 
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE 
}; 
const char *card_lookup[] = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}; 
const char *suit_lookup[] = {"C", "D", "H", "S" }; 

class Card { 
    public: 
     Card::Card() 
     { } 
     Card::Card(Rank rank, Suit suit) 
     { 
      this->rank = rank; 
      this->suit = suit; 
     } 
     Card::~Card() 
     {} 
     Rank Card::GetRank() const 
     { 
      return rank; 
     } 

     Suit Card::GetSuit() const 
     { 
      return suit; 
     } 
     string Card::ToString() { 
      string cardName = card_lookup[rank]; 
      cardName += suit_lookup[suit]; 
      return cardName; 
     } 
    private: 
     Rank rank; 
     Suit suit; 
}; 

bool operator<(const Card& lhs, const Card& rhs) 
{ 
    if(lhs.GetRank() == rhs.GetRank()) 
     return lhs.GetSuit() < rhs.GetSuit(); 
    else 
     return lhs.GetRank() < rhs.GetRank(); 

} 

const int CARDS_IN_HAND = 5; 
class Hand { 
    public: 
     Hand::Hand(int playerID) 
     { 
      this->playerID = playerID; 
     } 
     Hand::~Hand() 
     { } 

     void Hand::AddCard(Card newCard) 
     { 
      cards.push_back(newCard); 
     } 
     string Hand::ToString() 
     { 
      sort(cards.begin(), cards.end()); 

      stringstream playerCards; 
      playerCards << "Player " << this->playerID << " -"; 
      for (auto it = cards.begin(), end = cards.end(); it != end; ++it) 
       playerCards << " " << it->ToString(); 

      return playerCards.str(); 
     } 

    private: 
     int playerID; 
     vector<Card> cards; 

}; 


int main() { 
    vector<Hand> hands; 
    Hand h(1); 
    h.AddCard(Card(SIX, DIAMONDS)); 
    h.AddCard(Card(TEN, HEARTS)); 
    h.AddCard(Card(SEVEN, DIAMONDS)); 
    h.AddCard(Card(FOUR, CLUBS)); 
    h.AddCard(Card(NINE, SPADES)); 
    hands.push_back(h); 
    cout << hands[0].ToString() << endl; 
    return 0; 
} 
相關問題