我正在尝试创建一个卡片组阵列,其中包含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;
}
}
答案 0 :(得分:1)
循环的上限是错误的。而且你不能只是将数组分配给另一个数组。
添加此#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);
}