gpt4 book ai didi

C++ Array of Arrays,分别定义部分

转载 作者:太空狗 更新时间:2023-10-29 19:54:33 25 4
gpt4 key购买 nike

我正在尝试制作一个包含 2 个字符串列表(一个单数和一个复数)的数组。

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

我不知道执行此操作的正确语法,我想将其保留为 2 个数组的数组,这样我就可以使用如下代码:

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

谢谢。 :)

最佳答案

您走在正确的轨道上。您只需要一个声明,并嵌套括号以便您拥有一个数组数组:

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

关于C++ Array of Arrays,分别定义部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com