C++创建一个指向不同数组的数组
该程序的输入如下:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
我希望n
是指向其他整数数组的数组。因此,n
本质上应为{{1,5,4},{1,2,8,9,3}}}
。如果我想访问0个数组和第一个索引,则该值应返回5
,如果我要访问第一个数组和第三索引,则该值应为9 。
但是,此代码返回的值是32764
和32764
。
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n_l; // integer variable that will hold the length of array n
int q_l; // integer variable that will hold the length of the number of queries
cin >> n_l >> q_l; // assigns values to the variables n_l and q_l
int *n[n_l]; // creates an array that will contain pointers
for (int i = 0; i < n_l; i++){ // loops through the length of array n
int amount; // declares the variable amount
cin >> amount; // assigns a value to the variable amount
int k[amount]; // creates one of the arrays that will be added to n
for (int x= 0; x < amount; x++){ // loops through the length of k and assigns a value to each index
cin >> k[x];
}
n[i] = k; // adds the array k to the position in array n
}
for (int i = 0; i < q_l; i++){
int arraynum;
int index;
cin >> arraynum >> index;
cout << n[arraynum][index] << endl;
}
}
The inputs to this program are as follows:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
I would like n
to be an array that points to other integer arrays. So, n
should essentially be {{1, 5, 4}, {1, 2, 8, 9, 3}}
. If I wanted to access the 0th array and the 1st index, the value should return 5
, and if I were to access the 1st array and the 3rd index, the value should be 9
.
However, the values that this code returns are 32764
and 32764
.
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n_l; // integer variable that will hold the length of array n
int q_l; // integer variable that will hold the length of the number of queries
cin >> n_l >> q_l; // assigns values to the variables n_l and q_l
int *n[n_l]; // creates an array that will contain pointers
for (int i = 0; i < n_l; i++){ // loops through the length of array n
int amount; // declares the variable amount
cin >> amount; // assigns a value to the variable amount
int k[amount]; // creates one of the arrays that will be added to n
for (int x= 0; x < amount; x++){ // loops through the length of k and assigns a value to each index
cin >> k[x];
}
n[i] = k; // adds the array k to the position in array n
}
for (int i = 0; i < q_l; i++){
int arraynum;
int index;
cin >> arraynum >> index;
cout << n[arraynum][index] << endl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在C ++中不允许这样做。数组变量的大小必须是编译时间常数。您可以创建动态数组。最方便的方法是使用标准库中的
std :: vector
类模板。指针的问题在于,您在循环中创建的自动阵列会在循环陈述结束时自动销毁,而阵列中的指针都是悬空(即无效的)指针,以销毁被摧毁的阵列。当您以后间接通过无效的指针时,该程序的行为是不确定的。
您需要多个阵列。创建多个对象的好方法是什么?数组是创建多个对象的好方法。因此,为了创建多个数组,您可以创建一个数组数组。或者,由于您需要动态大小,因此可以创建向量的向量。这是如何创建向量向量的一个示例:
您可以为向量向量的向量内的数组中的数组创建指向向量的向量,但这是毫无意义的。您不需要指针。
This isn't allowed in C++. The size of an array variable must be compile time constant. You can create dynamic arrays. Most convenient way is to use
std::vector
class template from the standard library.The issue with your pointers is that the automatic arrays that you create in the loop are automatically destroyed at the end of the loop statement and the pointers in the array are all dangling (i.e. invalid) pointers to destroyed arrays. When you later indirect through the invalid pointers, the behaviour of the program is undefined.
You want multiple arrays. What's a good way to create multiple objects? Array is a good way to create multiple objects. So, in order to create multiple arrays, you can create an array of arrays. Or, since you want dynamic sizes, you can create a vector of vectors. Here is an example of how to create a vector of vectors:
You could create a vector of pointers to the arrays within the vectors in the vector of vectors, but that would be pointless. You don't need the pointers.
您正在定义内部循环中的数组,其范围将在该循环中受到限制,请尝试使用
new
进行数组分配区域,并将新分配区域的地址保存到指针阵列中。完成后,不要忘记删除分配的区域:
You are defining your array inside for loop which its scope will be limited in that loop, try allocate area with
new
for array and save the address of newly allocated area to your pointer array.After you’re done with it, don’t forget to delete allocated area:
对于开启器而言,可变长度数组不是标准的C ++功能
,而是可以使用
std :: vector&lt; int *&gt;
。或者,您可以使用std :: vector&lt; std :: pair&lt&lt&lt&int *&gt;&gt;
,其中一对的第一个元素存储了动态分配的数组中的元素数量和第二个元素这对存储了动态分配的数组的指针。此外,该数组将包含无效的指针,因为在退出for循环后,在for循环中声明的数组
将不会活着。
因此,至少您必须在for循环中动态分配数组。
并且您需要在访问动态分配的数组的元素之前检查输入的索引是否对给定数组有效。
For starters variable length arrays are not a standard C++ feature
Instead you could use
std::vector<int *>
. Or you could usestd::vector<std::pair<int, int *>>
where the first element of the pair stores the number of elements in the dynamically allocated array and the second element of the pair stores the pointer to the dynamically allocated array.Moreover the array will contain invalid pointers because the arrays declared in the for loop
will not be alive after exiting the for loop.
So at least you have to allocate dynamically arrays in the for loop.
And you need to check whether an entered index is a valid for a given array before accessing an element of the dynamically allocated array.