【问题标题】:How could i get this Multidimensional array to work?我怎样才能让这个多维数组工作?
【发布时间】:2017-10-19 09:33:50
【问题描述】:
    string shopInventory[4][2] = {
    {"Boots", "70"},
    {"Sword", "150"},
    {"Armor", "250"},
    {"Shield", "450"}
};
for (int i = 0; i < 4; i++) {
    for(int j = 0; j < 2; j++) {
        cout << "Multidimensional Array: " << shopInventory[i][NULL] << ": " << shopInventory[NULL][j] << endl;
    }
}

我正在尝试制作一个基本的商店系统,但我目前正纠结于如何输出带有细节的数组。

预期输出:

靴子:70 剑:150 护甲:250 护盾:450

实际输出:

多维数组:靴子:靴子 多维数组:靴子:70 多维阵列:剑:靴子 多维阵法:剑:70 多维数组:护甲:靴子 多维阵列:护甲:70 多维阵列:盾牌:靴子 多维阵列:护盾:70

还有没有办法让我根据用户想要购买的东西从数组中删除元素?

【问题讨论】:

  • 你想做什么?为什么使用空 pointer 常量作为索引?您是否尝试打印,例如shopInventory[i][j]?

标签: c++ arrays multidimensional-array element


【解决方案1】:

你把它复杂化了。你的循环应该是这样的:

for (int i = 0; i < 4; i++) {
    std::cout << "Multidimensional Array: " << shopInventory[i][0] << ": " << shopInventory[i][1] << std::endl;
}

并且不要像那样使用NULL - 如果你想在某处放一个零,请使用0

【讨论】:

    【解决方案2】:

    您可以通过以下方式输出数组,例如

    std::cout << "Multidimensional Array:" << std::endl;
    for ( size_t i = 0; i < sizeof( shopInventory ) / sizeof( *shopInventory ); i++ ) 
    {
        std::cout << shopInventory[i][0] << ": " << shopInventory[i][1] << std::endl;
    }
    

    或者你可以通过以下方式进行

    std::cout << "Multidimensional Array:" << std::endl;
    for ( const auto &item : shopInventory ) 
    {
        std::cout << item[0] << ": " << item[1] << std::endl;
    }
    

    请注意,除了二维数组之外,您还可以声明std::pair&lt;std::string, std::string&gt; 类型的对象的一维数组。例如

    std::pair<std::string, std::string> shopInventory[] =
    {
        { "Boots", "70" },
        { "Sword", "150" },
        { "Armor", "250" },
        { "Shield", "450" }
    };
    
    std::cout << "Multidimensional Array:" << std::endl;
    for ( size_t i = 0; i < sizeof( shopInventory ) / sizeof( *shopInventory ); i++ )
    {
        std::cout << shopInventory[i].first << ": " << shopInventory[i].second << std::endl;
    }
    

    std::pair<std::string, std::string> shopInventory[] =
    {
        { "Boots", "70" },
        { "Sword", "150" },
        { "Armor", "250" },
        { "Shield", "450" }
    };
    
    std::cout << "Multidimensional Array:" << std::endl;
    for (const auto &item : shopInventory)
    {
        std::cout << item.first << ": " << item.second << std::endl;
    }
    

    要使用标准类std::pair,您必须包含标题&lt;utility&gt;

    对于您的任务,如果您要从序列中删除元素,最好至少使用以下容器而不是数组

    std::vector<std::array<std::string, 2>>
    

    这是一个演示程序

    #include <iostream>
    #include <vector>
    #include <string>
    #include <array>
    
    int main()
    {
        std::vector<std::array<std::string, 2>> shopInventory =
        {
            { "Boots", "70" },
            { "Sword", "150" },
            { "Armor", "250" },
            { "Shield", "450" }
    
        };
    
        for (const auto &item : shopInventory)
        {
            std::cout << item[0] << ": " << item[1] << std::endl;
        }
    
        return 0;
    }
    

    它的输出是

    Boots: 70
    Sword: 150
    Armor: 250
    Shield: 450
    

    根据您要对集合执行的操作,考虑使用例如std::map 等关联容器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多