[英]c++ Variable Sized Arrays
i am working on an exercise in which:我正在做一个练习,其中:
As input: The first line contains two space-separated integers denoting the respective values of (the number of variable-length arrays) and (the number of queries).作为输入:第一行包含两个空格分隔的整数,分别表示(可变长度数组的数量)和(查询的数量)的值。 Each line of the subsequent lines contains a space-separated sequence in the format ka[i]0 a[i]1 … a[i]k-1 describing the -element array located at .
后续行的每一行都包含一个空格分隔的序列,格式为 ka[i]0 a[i]1 … a[i]k-1 描述位于 的 -element 数组。 Each of the subsequent lines contains two space-separated integers describing the respective values of (an index in array ) and (an index in the array referenced by ) for a query.
随后的每一行都包含两个以空格分隔的整数,分别描述查询的(数组中的索引)和(数组中引用的索引)的相应值。
sample input: 2 2 3 1 5 4 5 1 2 8 9 3 0 1 1 3样本输入:2 2 3 1 5 4 5 1 2 8 9 3 0 1 1 3
For output: For each pair of and values (ie, for each query), print a single integer denoting the element located at index of the array referenced by .对于输出:对于每对 和 值(即,对于每个查询),打印一个整数,表示位于由 引用的数组索引处的元素。 There should be a total of lines of output.
总共应该有几行输出。
sample out put: 5 9样品输出:5 9
And my code is:我的代码是:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n=0; int q=0;
cin>>n>>q;
vector<vector<int>> a;
for(int i=0; i<n;i++){
int k;
cin>>k;
for(int j=0; j<k;j++){
cin>>a[i][j];
}
}
for(int i=0; i<q;i++){
int x; int y;
cin>>x>>y;
cout<<a[x][y]<<endl;
}
cout<<n<<q;
return 0;
} }
But when i put input, I get an error但是当我输入时,我收到一个错误
A std::vector
defaults to empty. std::vector
默认为空。 It is not valid to call operator[]
with an expression like a[i]
on an empty vector
.在空
vector
上使用a[i]
类的表达式调用operator[]
是无效的。
You need to add elements to a vector
using functions such as push_back
or resize
.您需要使用
push_back
或resize
等函数向vector
添加元素。
Reserve space for the vector.为向量保留空间。
vector<int> integers( 100 );
See std::vector for details.有关详细信息,请参阅std::vector 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.