C++ 如何创建带有字符串和浮动的二维数组?

C++ 如何创建带有字符串和浮动的二维数组?,c++,arrays,multidimensional-array,data-structures,C++,Arrays,Multidimensional Array,Data Structures,我的infie.txt如下所示: Takoma_store 2.7 71.3 14.7 23.9 51.2 Bethesda_store 12.7 8.9 17.8 7.9 18.3 Baltimore_store 123.5 134.8 564.6 451.8 521.9 1796.6 District_store 56.2 26.5 123.4 456.7 789.3 1452.1 Prince_store 23.1 28.3 12.9 120.0 45.8 230.1 Columbia_s

我的infie.txt如下所示:

Takoma_store 2.7 71.3 14.7 23.9 51.2
Bethesda_store 12.7 8.9 17.8 7.9 18.3
Baltimore_store 123.5 134.8 564.6 451.8 521.9 1796.6
District_store 56.2 26.5 123.4 456.7 789.3 1452.1
Prince_store 23.1 28.3 12.9 120.0 45.8 230.1
Columbia_store 21.5 123.0 80.9 99.0 91.20 415.60
Bowie_store 100.0 100.0 100.0 100.0 100.0 100.0
for (int x = 0; x < number_of_stores; x++) {
    for (int y = 0; y < number_of_sales; y++) {
        //collect data from file
    }
}
我需要创建一个如下所示的数组

[Takoma_store] [2.7, 71.3, 14.7, 23.9, 51.2]
[Bethesda_store] [12.7, 8.9, 17.8, 7.9, 18.3]
[Baltimore_store] [123.5, 134.8, 564.6, 451.8, 521.9, 1796.6]
[District_store] [56.2, 26.5, 123.4, 456.7, 789.3, 1452.1]
[Prince_store] [23.1, 28.3, 12.9, 120.0, 45.8, 230.1]
[Columbia_store] [21.5, 123.0, 80.9, 99.0, 91.20, 415.60]
[Bowie_store] [100.0, 100.0, 100.0, 100.0, 100.0, 100.0]
使用两个for循环。我知道它需要格式化如下:

Takoma_store 2.7 71.3 14.7 23.9 51.2
Bethesda_store 12.7 8.9 17.8 7.9 18.3
Baltimore_store 123.5 134.8 564.6 451.8 521.9 1796.6
District_store 56.2 26.5 123.4 456.7 789.3 1452.1
Prince_store 23.1 28.3 12.9 120.0 45.8 230.1
Columbia_store 21.5 123.0 80.9 99.0 91.20 415.60
Bowie_store 100.0 100.0 100.0 100.0 100.0 100.0
for (int x = 0; x < number_of_stores; x++) {
    for (int y = 0; y < number_of_sales; y++) {
        //collect data from file
    }
}
for(int x=0;x

但我不知道如何声明一个多维(2D)数组,该数组允许我收集字符串(商店名称)和浮动(销售)如果您不允许使用
std::map
,如您在评论中所说,您可以使用struct执行以下操作:

struct strdbl
{
    std::string name;
    double nums[6];
};

int main()
{
    strdbl sf[10] = {
        {"Takoma_store", {2.7, 71.3, 14.7, 23.9, 51.2}},
        {"Bethesda_store", {12.7, 8.9, 17.8, 7.9, 18.3}},
        {"Baltimore_store", {123.5, 134.8, 564.6, 451.8, 521.9, 1796.6}},
        {"District_store", {56.2, 26.5, 123.4, 456.7, 789.3, 1452.1}},
        {"Prince_store", {23.1, 28.3, 12.9, 120.0, 45.8, 230.1}},
        {"Columbia_store", {21.5, 123.0, 80.9, 99.0, 91.20, 415.60}},
        {"Bowie_store", {100.0, 100.0, 100.0, 100.0, 100.0, 100.0}}
    };

    return 0;
}

在Hamed的答案中使用类似的结构是可能的

另一种方法是使用,即
std::variant
,然后制作一个数组

使用C-Array并不是很好的C-plus-plussy。但是,如果你的老师喜欢顽皮的东西,你真的可以将
std::string
(或
char*
double
存储在同一个C数组中,如果你制作了一个
void*
数组,即:

char* store = "Takoma_store";
double d1   = 2.7;
double d2   = 71.3;
...
void* list[] = { &store, &d1, &d2 ...};
要访问,即
d1
您需要编写
double d=*(double*)列表[1]。天哪!非常讨厌

我想不出C++中有什么更脏的东西,你的老师可能会喜欢。
您还可以将
void*
数组设置为2D,并在一维中存储
char*
(或任何内容),在另一个维度中存储

更好的数据结构是使用
map
,或
无序的\u map
。使用
std::map
如何?像这样:
std::map
。如果你的名字不唯一,那么你可以使用
std::multimap
@YamanJain我试过这么做,但这是一个作业,老师让我使用数组。@Hamed我试过这么做,但这是一个作业,老师让我使用数组。然后我想你需要为它定义一个类或结构,我认为数组是不可能的,除非你的数字也是字符串,以后再转换。