简体   繁体   中英

Would this work? I'm trying to make a multi-dimensional array, and I'm pretty sure it would work in Java, but I don't know about C++

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"};

If it doesn't work, how could I make it work, or how could I make it work better?

You can't assign the internal arrays individually, but you can do this when defining the array:

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"}
};

No, it won't. First, you can't use initializers outside initialization. You should try something like this:

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"}};

Although, in new C++11 standard that's valid (with some changes), but only if you use std::vector . The second problem is that you try to assign an array to a string. Instead of months[1][12] , use 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"};

在C ++中是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM