I'm trying to make an array of arrays containing 2 lists of strings (one singular and one plural).
string item_name[2][6];
string item_name[0] = {"bag of CinnaCandies", "can of Arizona Tea", "Starbucks Mocha Frappe", "copy of Section 8: Prejudice", "Sushi Box", "pair of Nike Jordans"};
string item_name[1] = {"bags of CinnaCandies", "cans of Arizona Tea", "Starbucks Mocha Frappes", "copies of Section 8: Prejudice", "Sushi Boxes", "pairs of Nike Jordans"};
I don't know the proper syntax to do this and I'd like to keep it as an array of 2 arrays so I can have code that says:
if (quantity > 1)
{
cout << item_name[0][index];
}
else
{
开发者_如何学Go cout << item_name[1][index];
}
Thanks. :)
You're on the right track. You just need a single declaration, and nest the brackets so that you have an array of arrays:
string item_name[2][6] = {{"bag of CinnaCandies", "can of Arizona Tea",
"Starbucks Mocha Frappe", "copy of Section 8: Prejudice",
"Sushi Box", "pair of Nike Jordans"},
{"bags of CinnaCandies", "cans of Arizona Tea",
"Starbucks Mocha Frappes",
"copies of Section 8: Prejudice", "Sushi Boxes",
"pairs of Nike Jordans"}};
Initialise normally, but instead of a regular constant you use a sub-array:
string item_name[2][6] = {
{"bag of CinnaCandies", "can of Arizona Tea", "Starbucks Mocha Frappe", "copy of Section 8: Prejudice", "Sushi Box", "pair of Nike Jordans"},
{"bags of CinnaCandies", "cans of Arizona Tea", "Starbucks Mocha Frappes", "copies of Section 8: Prejudice", "Sushi Boxes", "pairs of Nike Jordans"}
};
Both answers above are correct. In addition, you also can use something like this:
vector<string> item_name[2];
This way, you still have an array of "arrays" but the sub-array size is not fixed, so you can continue appending as many entries as you need.
And of course, you can also use the notation
item_name[0][index];
To get the "index" item at the first top-level array.
精彩评论