gpt4 book ai didi

What does int** outer = new int*[n] in this code mean? [closed](在此代码中,int**out=new int*[n]是什么意思?[已关闭])

转载 作者:bug小助手 更新时间:2023-10-25 20:11:13 32 4
gpt4 key购买 nike




I just started learning programming and c++. I can't seem to wrap my head around pointers and variable sized array. I didn't know how to solve this problem on hackerrank so I tried to look and understand the solution in the editorial.

我刚刚开始学习编程和c++。我似乎无法理解指针和可变大小的数组。我不知道如何在HackerRank上解决这个问题,所以我试图查看和理解社论中的解决方案。


So I think the code is supposed to take input of n number of arrays, q numbers of queries, the size of and values in each arrays, and find/query xth data in yth array (for example if the input is 0 1, then we need to find the value of array[0][1]).

因此,我认为代码应该接受n个数组的输入、Q个查询数、每个数组的大小和值,并在第y个数组中查找/查询第x个数据(例如,如果输入是0 1,则需要找到数组[0][1]的值)。


Suppose that the input is:

假设输入为:


2 2

2 2


3 1 5 4

3 1 5 4


5 1 2 8 9 3

5 1 2 8 9 3


0 1

0 1


1 3

十三


The output should be:

输出应为:


5

5.


9

9.


2 arrays so there are :

2个阵列,因此有:


array[0][] with the size 3 and values of {1, 5, 4}

大小为3且值为{1,5,4}的数组[0][]


array[1][] with the size of 5 and values of {1, 2, 8, 9, 3}

数组[1][],大小为5,值为{1,2,8,9,3}


2 queries so we need to look for the values in:

2个查询,因此我们需要在以下位置查找值:


array[0][1] which is 5

数组[0][1],即5


array[1][3] which is 9

数组[1][3],即9


But what I don't understand is how do I make arrays of arrays, and why do I have to use pointers and how do I do that? The solution also includes int** which I think means pointer to pointer. What does that even mean??? And how do I use new int? Why does the new int is also a pointer (new int*[n])??

但我不明白的是,我如何制作数组的数组,为什么我必须使用指针,我如何做到这一点?该解决方案还包括int**,我认为这意味着指向指针的指针。这到底是什么意思?我如何使用新的int?为什么新的int也是一个指针(new int*[n])?


int main() {
int n;
int q;
cin >> n >> q;

int** outer = new int*[n];

for(int i = 0; i < n; i++) {
int k;
cin >> k;

outer[i] = new int[k];

for(int j = 0; j < k; j++) {
cin >> outer[i][j];
}
}


while(q-- > 0) {
int outer_index;
int inner_index;
cin >> outer_index >> inner_index;

cout << outer[outer_index][inner_index] << endl;
}

return 0;
}

I am clueless. pls help.

我一点头绪都没有。请帮帮忙。


更多回答

new is an advanced tool, depsite how some bad tutorials make it look like. You will rarely use it in your job. Most of the time you'll use std::vector<std::vector<int>> as a 2D array, or even better just a 1D std::vector<int> of size m*n. Of course you still need to understand how pointers and new work, your C++ book will explain that. But you don't need to jump to them to solve a simple exercise.

New是一个高级工具,详细描述了一些糟糕的教程是如何让它看起来像这样的。你很少在工作中用到它。大多数情况下,您会将std::VECTOR用作2D数组,或者更好的做法是只使用大小为m*n的1D STD::VECTOR。当然,您仍然需要了解指针和新的工作原理,您的C++书将对此进行解释。但你不需要跳到他们那里去解决一个简单的练习。

@jwryd - What you might learn from this is that hackerrank is a terrible place to learn C++ (or programming in general). Despite some popular culture memes, good software developers don't consider themselves hackers. We are way above that level! :-)

@jwryd -你可能从中学到的是,hackerrank是一个学习C++(或一般编程)的可怕地方。尽管有一些流行文化模因,但优秀的软件开发人员并不认为自己是黑客。我们远远超过这个水平!:-)

Does this answer your question? What does the new operator returns?

这回答了你的问题吗?新运算符返回什么?

It means the code is still using "C" style memory management. Where are you learning C++ from? Your source material seems at least 12 years out of date (possibly longer)

这意味着代码仍在使用“C”风格的内存管理。您是从哪里学习C++的?您的原始材料似乎至少过时了12年(可能更长)

This is what current C++ material should teach you to use : onlinegdb.com/hVS75HARMQ

这是当前的C++材料应该教你如何使用的:onlinegdb.com/hVS75HARMQ

优秀答案推荐

You know that int* a; defines the variable a as a pointer to an int, right? It's the same with more asterisks, it's add more pointers. So int** a; defines a as a pointer to a pointer to an int.

您知道,int*a;将变量a定义为指向int的指针,对吗?星号越多也是一样,就是增加更多的指针。因此,int**a;将定义为指向int指针的指针。


And new int*[n] creates an array of n elements, where each element is an int*. Since new returns a pointer to what's created, a pointer to int* must then be int**.

而new int*[n]创建一个由n个元素组成的数组,其中每个元素都是一个int*。由于new返回指向所创建内容的指针,因此指向int*的指针必须为int**。




If we take the time to "draw" what's happening, then outer = new int*[n] would create something like this:

如果我们花时间“绘制”正在发生的事情,那么out=new int*[n]将创建如下所示:



+--------------+
| outer[0] | --> ?
+--------------+
| outer[1] | --> ?
+--------------+
| ... |
+--------------+
| outer[n - 1] | --> ?
+--------------+

So you have an array of pointers. The elements in this array are not initialized, they don't point anywhere valid. Which is why the arrows are pointing to the question mark ?.

所以你有一个指针数组。此数组中的元素未初始化,它们没有指向任何有效的位置。这就是为什么箭头指向问号的原因?


Then inside the loop you have the statement

然后在循环中有这样一条语句


outer[i] = new int[k];

It allocates an array of int elements.

它分配一个int元素数组。


After the loop the drawing then becomes something like this instead:

在循环之后,图形会变成如下所示:



+--------------+ +-------------+-------------+-----+-----------------+
| outer[0] | --> | outer[0][0] | outer[0][1] | ... | outer[0][k - 1] |
+--------------+ +-------------+-------------+-----+-----------------+
| outer[1] | --> | outer[1][0] | outer[1][1] | ... | outer[1][k - 1] |
+--------------+ +-------------+-------------+-----+-----------------+
| ... |
+--------------+ +-----------------+-----------------+-----+---------------------+
| outer[n - 1] | --> | outer[n - 1][0] | outer[n - 1][1] | ... | outer[n - 1][k - 1] |
+--------------+ +-----------------+-----------------+-----+---------------------+

I hope this helps you understand what's going on.

我希望这能帮助你了解正在发生的事情。


更多回答

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com