如何在c ++中声明3维字符串数组

时间:2017-10-18 13:29:38

标签: c++ arrays

#include<stdio.h>

char count[3][5][14]={{"♠1","♠2","♠3","♠4","♠5","♠6","♠7","♠8","♠9","♠10","♠J","♠Q","♠K"},
                            {"◇1","◇2","◇3","◇4","◇5","◇6","◇7","◇8","◇9","◇10","◇J","◇Q","◇K"},
                            {"♣1","♣2","♣3","♣4","♣5","♣6","♣7","♣8","♣9","♣10","♣J","♣Q","♣K"},
                            {"♡1","♡2","♡3","♡4","♡5","♡6","♡7","♡8","♡9","♡10","♡J","♡Q","♡K"};

我想声明这种类型的数组,但它总是会产生错误,例如&#34;太多的初始化器和#34;。我该如何解决这个错误?

2 个答案:

答案 0 :(得分:3)

你想做的就是这个:

const char * count[4][13]= {{"♠1","♠2","♠3","♠4","♠5","♠6","♠7","♠8","♠9","♠10","♠J","♠Q","♠K"},
                            {"◇1","◇2","◇3","◇4","◇5","◇6","◇7","◇8","◇9","◇10","◇J","◇Q","◇K"},
                            {"♣1","♣2","♣3","♣4","♣5","♣6","♣7","♣8","♣9","♣10","♣J","♣Q","♣K"},
                            {"♡1","♡2","♡3","♡4","♡5","♡6","♡7","♡8","♡9","♡10","♡J","♡Q","♡K"}};

无论如何已经建议std::vectorstd::string应该是首选的

答案 1 :(得分:2)

C ++方式将使用类似std::string的字符串类和类似std::vector的容器(不是原始的C样式char字符串和原始数组),例如:

 vector<vector<vector<string>>> x;

如果你真正想要的是一个二维字符串数组,那就是:

 vector<vector<string>> x;