这会有用吗?我正在尝试创建一个多维数组,我很确定它可以在Java中工作,但我不知道C ++

时间:2012-02-20 04:07:52

标签: java c++ multidimensional-array

string months[3][12];
months[0][12] = {"January", "February", "March", "April", 
    "May", "June", "July", "August", "September", "October", 
    "November", "December"};
months[1][12] = {"january", "february", "march", "april", 
    "may", "june", "july", "august", "september", "october", 
    "november", "december"};
months[2][12] = {"1", "2", "3", "4", "5", "6", "7", "8", 
    "9", "10", "11", "12"};

如果它不起作用,我怎么能让它工作,或者我怎样才能使它更好地工作?

3 个答案:

答案 0 :(得分:1)

您不能单独分配内部数组,但可以在定义数组时执行此操作:

string months[3][12] = {
    {"January", "February", "March", "April", 
    "May", "June", "July", "August", "September", "October", 
    "November", "December"},
    {"january", "february", "march", "april", 
    "may", "june", "july", "august", "september", "october", 
    "november", "december"},
    {"1", "2", "3", "4", "5", "6", "7", "8", 
    "9", "10", "11", "12"}
};

答案 1 :(得分:0)

不,它不会。首先,您不能在初始化之外使用初始化程序。你应该尝试这样的事情:

string months[3][12] = {{"January", "February", "March", "April", 
    "May", "June", "July", "August", "September", "October", 
    "November", "December"}, {"january", "february", "march", "april", 
    "may", "june", "july", "august", "september", "october", 
    "november", "december"}, {"1", "2", "3", "4", "5", "6", "7", "8", 
    "9", "10", "11", "12"}};

虽然在新的C ++ 11标准中有效(有一些更改),但只有在使用std::vector时才有效。第二个问题是您尝试将数组分配给字符串。而不是months[1][12],请使用months[1]

vector<vector<string>> months(3);
months[0] = {"January", "February", "March", "April",
    "May", "June", "July", "August", "September", "October",
    "November", "December"};
months[1] = {"january", "february", "march", "april",
    "may", "june", "july", "august", "september", "october",
    "november", "december"};
months[2] = {"1", "2", "3", "4", "5", "6", "7", "8",
    "9", "10", "11", "12"};

答案 2 :(得分:0)

在C ++中是:

int mat[][]={{1,2,3,4}, {5,6,7,8}, {9,10,11,12},};