如果您真的想使用原始数组,则必须动态分配它,即在堆上:
int main(){
int n = 0;
std::cin>>n;
int** lh = new int[n][2];
// Allocate memory
lh = new int*[n];
for (int i = 0; i < n; ++i)
lh[i] = new int[2];
// Assign values
lh[0][0] = 3;
lh[1][2] = 4;
// De-Allocate memory to prevent memory leak
for (int i = 0; i < n; ++i)
delete [] lh[i];
delete [] lh;
return 0;
}
但是,由于您可能还没有准备好使用多维原始堆数组,我强烈建议您改用std::vector
更小的数组大小:
int main(){
int n = 0;
std::cin>>n;
std::vector<std::vector<int> > myVec;
for(int i = 0; i < n; i++)
{
std::vector<int> tempV;
int input = 0;
cin >> input;
tempV.push_back(input);
cin >> input;
tempV.push_back(input);
myVec.push_back(std::move(tempV));
}
return 0;
}