简体   繁体   English

2D可变大小数组C ++

[英]2D variable sized array c++

I wanted to get familiar with 2D variable sized arrays in c++, so I wrote a little program, but it doesn't work. 我想熟悉c ++中的2D可变大小数组,所以我写了一个小程序,但是没有用。 Here is the code: 这是代码:

#include <iostream>

using namespace std;

int main(){
int a,i;
cin>>a; //the width of the array is variable
int **p2darray;
p2darray = new int*[2]; //the height is 2
for (i = 0; i < 2; i++){
    p2darray[i] = new int[a];
}
i=0;
while(i!=a){
    p2darray[0][i]=i; //filling some numbers in the array
    p2darray[1][i]=2*i;
    i++;
}
i=0;
while(i!=a){
    cout<<p2darray[0][i]<<endl;
    cout<<p2darray[1][i]<<endl;
    i++;
}
return 0;
}

So why doesn't it work? 那为什么不起作用呢?

The main problem is that when you say p2darray[i][0] , your indices are backwards because you set the second dimension to the size the user enters, but you're incrementing the first dimension to that number instead. 主要问题是,当您说p2darray[i][0] ,索引向后,因为您将第二个维设置为用户输入的大小,但是您将第一个维增加为该数字。 This would normally cause a segfault. 这通常会导致段错误。 It should be p2darray[0][i] in all four cases. 在所有四种情况下都应为p2darray[0][i] You also didn't set i back to 0 before entering the printing loop, so it's skipping the entire printing process. 您也没有在进入打印循环之前将i设置为0,因此它跳过了整个打印过程。

For a running program that illustrates the said corrections, see here . 有关说明上述更正的正在运行的程序, 请参见此处

You forgot to reset i . 你忘了重设i

i=0;
while(i!=a){
    p2darray[i][0]=i; //filling some numbers in the array
    p2darray[i][1]=2*i;
    i++;
}
// Now i == a, so the next loop doesn't run
while(i!=a){
    cout<<p2darray[i][0]<<endl;
    cout<<p2darray[i][1]<<endl;
    i++;
}

Insert i = 0; 插入i = 0; between the two loops. 在两个循环之间。

Also, you have the indices in the wrong order, the first index can only take the values 0 and 1, otherwise you access memory outside the allocated area. 同样,索引的顺序错误,第一个索引只能采用值0和1,否则将访问分配区域之外的内存。

i=0;
while(i!=a){
    p2darray[0][i]=i; //filling some numbers in the array
    p2darray[1][i]=2*i;
    i++;
}
i = 0;
while(i!=a){
    cout<<p2darray[0][i]<<endl;
    cout<<p2darray[1][i]<<endl;
    i++;
}

is correct. 是正确的。

Why don't you place the 2d array in to a function. 为什么不将2d数组放入函数中。

#include <iostream>

using namespace std;

int** CreateArray(int valuea, int valueb)
{
     int** array;
     array = new int *[valuea];
     for(int i =0;i< row;i++)
     {
         array[i] = new int [valueb];
     }
     return array;
}

Then you must not forget to return the memory. 然后,您一定不要忘记返回内存。

int main(){
int a;
int i = 0;

cin>>a;
int** array = CreateArray(a,i);

while(i!=a){
  array[i][0]=i;
  array[i][1]=2*i;
  i++;
}
i=0;
    while(i!=a){
      cout<<array[i][0]<<endl;
      cout<<array[i][1]<<endl;
      i++;
 }
return 0;
}

A 2D variable size Array in C++ C ++中的2D可变大小数组

 #include <bits/stdc++.h>
 using namespace std;

 int main(){

int row,col,i,j;
cin>>row;                    //no. of rows
string col_size;             //mapping index to no.of columns     

vector<vector<int> >Arr(row);

for(i=0; i<row ; i++){
  cin>>col;
  col_size.push_back(col);    // no. of columns at ith row 
  Arr[i]=vector<int>(col);
       for(j=0 ; j < col_size[i] ; j++)
             cin>>Arr[i][j];
}

                      //printing the 2D Array

for(i=0; i<row ; i++){
       for(j=0 ; j<col_size[i] ;j++){
              cout<<Arr[i][j]<<" ";
   }
    cout<<"\n";
   }
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM