我正在尝试创建一个包含52个卡片结构的卡片组数组,这些卡片结构包含一个整数和一个“ pic”,这将是西服的unicode表示形式。我尝试用unicode值为每套西装创建一个常量字符,但这不太起作用。我需要获得一个unicode库吗?这是我的代码:

#ifndef CARD_H
#define CARD_H
#include <iostream>
using namespace std;

class Card
{

private:

struct OneCard
{
    int value;
    char Pic[4];
};

OneCard Cards[52];



public:
Card();

 };

#endif



#include "Card.h"
#include <iostream>
using namespace std;

Card::Card()
{
const char spade[4]="\xe2\x99\xa0";
const char club[4]="\xe2\x99\xa3";
const char heart[4]="\xe2\x99\xa5";
const char diamond[4]="\xe2\x99\xa6";

for (int i = 0; i<13; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=spade;
}

for (int i = 13; i<26; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=club;
}
for (int i = 26; i<39; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=heart;
}
for (int i = 39; i<52; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=diamond;
}


}

最佳答案

您的循环上限是错误的。而且,您不能仅将一个数组分配给另一个数组。

添加此#include <algorithm>并更改为以下代码:

for (int i = 0; i<13; i++)
{
    Cards[i].value=i+1;
    std::copy(spade, spade+3, Cards[i].Pic);
}

for (int i = 13; i<26; i++)
{
    Cards[i].value=i+1;
    std::copy(club, club+3, Cards[i].Pic);
}
for (int i = 26; i<39; i++)
{
    Cards[i].value=i+1;
    std::copy(heart, heart+3, Cards[i].Pic);
}
for (int i = 39; i<52; i++)
{
    Cards[i].value=i+1;
    std::copy(diamond, diamond+3, Cards[i].Pic);
}

关于c++ - 使用unicode初始化卡片组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20108615/

10-13 08:18