【发布时间】:2019-12-19 01:45:58
【问题描述】:
我正在尝试解决可变大小数组上的这个问题,但我遇到了编译错误。不完全确定我哪里出错了。
Problem can be accessed in this PDF
我的解决方案尝试如下:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q,size,elem,index,smallindex;
//input of n and q
cin>>n>>q;
//declare the array a
int* bigarr = new int[q];
//assign the individual arrays to each element of the array a
for (int i=0; i<n; ++i){
//input of size of the individual array
cin>>size;
int* smallarr = new int[size];
for (int j=0; j<size; ++j){
smallarr[j] = cin>>elem;
}
bigarr[i] = smallarr;
}
//obtain index queries
for (int k=0; k<n; ++k){
cin>>index;
cin>>smallindex;
cout<<bigarr[index][smallindex];
}
}
【问题讨论】:
-
使用
std::vector解决问题。没有必要深入了解所有这些动态内存管理的杂草。您链接到的问题并不关心您使用什么底层结构,只要您能回答这个问题。请注意,使用vector并不能解决您所链接的问题 - 它确实让您可以专注于问题,而无需正确处理所有这些内存管理。跨度> -
@PaulMcKenzie 知道了,谢谢!
标签: c++ arrays multidimensional-array stdout stdin