第二行的列数较少"切断"第一行的列数更多

时间:2015-08-24 17:37:06

标签: c++ pointers vector multidimensional-array dynamic-memory-allocation

*编辑:当输入第1行的3列和第2行的2列时,输出的第1行变为2 - 作为第1行。

输出动态分配的配件数量与单独动态分配的列数(对于每个装备的捕获次数)的问题...即,如果我尝试分配2个equipes然后为第一个装备2个&#34 ;渔获"鱼(两列)和第二装备三条鱼,一切都很好......但如果我尝试输入较少数量的柱子("捕获")为第二排(装备),那么输出"多余"第一行的第#行切断",例如,如果第一行输入3列,第二行输入2列,则输出中只有两列(索引为数字)为两行中的每一行。

#include<iostream>

int main()
{
    using namespace std;

    int *sum;

    int *a = new int;
    int *b = new int;


    cout << "Total number of equips: ";
    cin >> *a;


    // Allocate a two-dimensional 3x2 array of ints
    int** ippArray = new int*[*a];
    for (int i = 0; i < *a+1; ++i) {
            ippArray[i] = new int[*b];
    }

    // fill the array

    for (int i = 1; i < *a+1; ++i) {
            cout << "Total number of catches for " << i << "th equip : ";
            cin >> *b;
            cout << "Equip number: " << i << endl;

            for (int j = 1; j < *b+1; ++j) {
                    cout << "Catch number: " << j << endl;
                    cin >> ippArray[i][j];
                    ippArray[i][j];
            }
    }

    // Output the array
    for (int i = 1; i < *a+1; ++i) {
        for (int j = 1; j < *b+1; ++j) {
                            cout << ippArray[i][j] << "  ";
                            *sum = *sum + ippArray[i][j];
                    }
                    cout << endl;
            }
            cout << endl;

            cout << "All catches of the all equipes: " << *sum-3;




            // Deallocate
            for (int i = 1; i < *a+1; ++i) {
                    delete [] ippArray[i];
            }
            delete [] ippArray;
            // Keep the window open
            cin.get();
            return 0;
        }

2 个答案:

答案 0 :(得分:1)

首先,除非确实需要,否则不要将整数变为指针(int *a = new int;)。它使代码更难阅读,如果有人必须维护你的代码,他们会称你为一个洞。

其次,int** ippArray = new int*[*a];与您执行此操作的多个位置相结合... for (int i = 1; i < *a+1; ++i)很糟糕。 ippArray具有从0到*a的有效引用,因此它应该是for (int i = 0; i < *a; ++i)

修改:尝试使用此类http://ideone.com/4egQl3

Edit2:也是标准建议......

{
    std::vector<string> advice;
    advice.push_back( "These will make your life easier" );
}
// No de-allocation needed!

答案 1 :(得分:0)

程序中具有未定义行为的部分

  • 在分配之前使用* b
  • 访问所有数组的越界元素
  • 永远不会初始化和
  • 在分配之前使用* sum

清理完毕,您的代码变为

int main()
{
    using namespace std;

    int sum, a, b;

    cout << "Total number of equips: ";
    cin >> a;

    typedef vector<vector<int> > vvint;
    typedef vector<int> vint;

    // Allocate a two-dimensional structure of ints
    vvint ippArray(a);

    // fill the array
    for (vvint::size_t i = 0; i < a; ++i) {
        cout << "Total number of catches for " << i+1 << "th equip : ";
        cin >> b;
        cout << "Equip number: " << i+1 << endl;

        ippArray[i] = vint(b);

        for (int j = 0; j < b; ++j) {
            cout << "Catch number: " << j+1 << endl;
            cin >> ippArray[i][j];
        }
    }

    // Output the array
    for (const vint & inner : ippArray) {
        for (int num : inner) {
            cout << num << "  ";
            sum += num;
        }
        cout << endl;
    }
    cout << endl;

    cout << "All catches of the all equipes: " << sum;
    cin.get();
    return 0;
}