簡體   English   中英

C ++枚舉值

[英]C++ Enum Values

我正在嘗試通過轉換我之前制作的一些Java類來學習C ++。 它們代表一張撲克牌和一副牌。 我正在使用enum來表示值和套裝:

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

enum Values{TWO, THREE, FOUR, FIVE, 
            SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};

在我的JavaC++ Card類中,我都有方法: getValue()getSuit() ,它們顯然分別返回值和適合。

我的Java DeckofCards類非常簡單:

public class DeckofCards {

private Card card; 
private String value;
private String suit;
private List<Card> deck = new ArrayList<Card>();

//DeckofCards constructor
public DeckofCards(){
    for (Suits s : Suits.values()) {
        for(Values v : Values.values()){
            card = new Card(v,s);
            deck.add(card);
        }  
    }
}

//shuffles the deck
public void shuffleDeck(){
    Collections.shuffle(deck);
}

//prints the deck of cards
public void printDeck(){

    for(int i = 0; i<deck.size(); i++){
        card = deck.get(i);
        value = card.getValue().toString();
        suit = card.getSuits().toString();
        System.out.println(value + " of " + suit);
    }

}

//draws a card from the deck
public Card drawCard(){
    try{
        card = deck.get(0);
        deck.remove(0);
        //return card;
    }
    catch(IndexOutOfBoundsException e){
        System.err.println("Deck is empty");
        System.exit(0);
    }
    return card;
}

} 

我的問題是在C ++中實現printDeck()方法,特別是獲取enum值的字符串表示。 我知道我不能簡單地做getValue().toString()所以我對這個問題進行一些研究之后的想法就是讓兩個std::string[]與兩個enum看起來相同然后使用getValue()getSuit()生成一個int(因為這似乎是行為)並將其傳遞給數組以獲取字符串表示。

但是我現在認為在我的Card類中再添加兩個方法可能會更好:

std::string getValue(int i)同樣適用於訴訟

並使用case語句返回基於intstring值,以便其他類可以輕松獲取字符串表示形式。 這似乎是多余的。 任何人都可以就如何做到這一點提出任何建議嗎?

您可以使用新的C ++ 11枚舉類(即作用域枚舉),並定義一個將此類枚舉類作為輸入參數的函數,並返回輸入枚舉值的相應字符串表示形式。

例如:

#include <assert.h>     // For assert()
#include <iostream>     // For console output
#include <string>       // For std::string

enum class Suits {
    SPADES, CLUBS, HEARTS, DIAMONDS
};

std::string toString(Suits s) {
    switch (s) {
    case Suits::SPADES:     return "Spades";
    case Suits::CLUBS:      return "Clubs";
    case Suits::HEARTS:     return "Hearts";
    case Suits::DIAMONDS:   return "Diamonds";
    default:
        assert(false);
        return "";
    }
}

int main() {
    std::cout << toString(Suits::CLUBS) << std::endl;
}

你可以為Values枚舉做類似的事情。

在C ++中,枚舉周圍沒有元數據,所以如果你想要一個字符串版本,你需要自己編寫一個轉換器。

我可能會用這樣的東西,這不會編譯,但你得到粗略的圖片。

enum Suit { ... }
enum Values { ... }

Class card {
public:
   static std::string getText(Suite s) { switch(s) case CLUBS: return "clubs"; ... }
   static std::string getText(Colour c) { switch(c) case ONE: return "one"; ... }

   card(Suite s, Colour c) : mSuite(s), mColour(c) {}
   std::string getText() const {
       stringstream ss;
       ss << getText(mColour) << " of " << getText(mSuits);
       return ss.str();
   }
};
ostream& operator<<(ostream& stream, const card& c) {
    stream << c.getText();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM