我有一個快速的問題,無法在其他地方找到答案。基本上我試圖做一個通用函數來返回正確的unicode(而不是文字),如下所示在std :: string getUnicode()函數中。 \ xe2 \ x99 \ xa和cardType在輸出中被視爲兩個單獨的字符串,這會導致「?」隨後是cardType號碼。C++ Universal Unicodes
在這種情況下:
cout << "\xe2\x99\xa0"; //prints out a symbol, GOOD
cout << "\xe2\x99\xa" << 0; //prints out "?" followed by 0. BAD
cout << card.getUnicode(); //prints out "?" followed by 0. BAD
任何想法? 4-6個月的初學者到C++。
#ifndef CARD_H
#define CARD_H
#include <map>
#include <sstream>
#include <string>
enum CARD_TYPE {SPADE = 0, CLUB = 3, HEART = 5, DIAMOND = 6};
class Card {
private:
int number;
CARD_TYPE cardType;
public:
Card(CARD_TYPE, int);
void displayCard();
int getNumber() {
return number;
}
CARD_TYPE getCardType() {
return cardType;
}
/* Returns Unicode Value for this Card Type */
std::string getUnicode() {
std::stringstream ss;
ss << "\xe2\x99\xa" << cardType;
return ss.str();
}
};
#endif
我不知道你想要什麼。什麼是「爲您返回適當的Unicode」?該代碼可以使* UTF8的某些*感覺...以及「輸出」在哪裏?請一個MVCE。 – deviantfan
cout << card.getUnicode(); //這就是「輸出」。對於心臟,黑桃,鑽石和俱樂部來說,unicode除了最後的數字外都是一樣的,我會動態地將相應的數字追加到最後。 – Gosre