An array of C ++ arrays that defines parts separately

I am trying to create 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 do not know the correct syntax to do this, and I would like to save it as an array of 2 arrays, so I can have code that says:

if (quantity > 1)
    {
        cout << item_name[0][index];
    }
    else
    {
        cout << item_name[1][index];
    }

Thank.:)

+3
source share
3 answers

Initialize normally, but instead of a regular constant, you use a helper 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"}
};
+2
source

You are on the right track. You just need one declaration and insert 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"}};
+6
source

. , - :

vector<string> item_name[2];

, "", , , .

, ,

item_name[0][index];

"index" .

+2
source

All Articles