【问题标题】:How to implement a variable multidimensional array using int** array = new int*[n];?如何使用 int** array = new int*[n]; 实现变量多维数组?
【发布时间】:2021-10-31 05:00:25
【问题描述】:

我尝试了这种方法,但是当我cout <<vararr[i][j]<<endl; 时出现错误 我要解决的问题-hackerrank

我的代码-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int* variablesizedarr(int size){
    int* arr= new int [size];
    for(int i=1;i<size;i++){
        cin >>arr[i];
    }
    /*I believe the problem is when I return the arr array*/
    return arr;
    
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n,q,size;
    cin >>n >>q;
    int** vararr= new int* [n];
    for(int i=0;i<n;i++){
        cin >>size;
        vararr[i]=variablesizedarr(size);
    }
    /*now printing the values to the console*/
    
    for(int k=0;k<q;k++){
        int i,j;
        cin >>i>>j;
        cout <<vararr[i][j]<<endl; /* difinetly an error in this line because that's what the compiler say*/
        
    } 
    // deallocate the array
    for(int i=0;i<n;i++){
        delete [] vararr[i];
        
    }
    delete [] vararr;
    
    return 0;
}

edit1:我遇到的错误-

Compiler Message
Segmentation Fault
Error (stderr)
Reading symbols from Solution...done.
[New LWP 2655054]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004011f2 in main () at Solution.cpp:32
32          cout <<vararr[i][j]<<endl;
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

我知道我可以像其他人一样做到这一点,但我不会那样学习。任何帮助表示赞赏。

【问题讨论】:

  • 请在问题中包含错误。
  • variablesizedarr 你为什么不写信给arr[0]
  • 另外,现在很难重现这一点,因为我们不知道您提供的是什么输入。你能用硬编码值替换所有cin &gt;&gt; n &gt;&gt; q; 类型的东西,并且仍然遇到你描述的问题吗?如果不能,你至少能告诉我们你正在尝试什么输入,你期望什么输出,以及你得到什么输出? “我遇到错误”非常模糊。
  • 我已经对其进行了编辑,还提到了我遇到的错误。
  • @NathanPierson 哎呀。我怀疑这会解决错误。让我试试

标签: c++ arrays


【解决方案1】:
int** p = new int*[n];
for (int i = 0; i < n; i++) {
    p[i] = new int[m];
}

【讨论】:

  • 如果我从函数中得到 p[i] 的值怎么办?因此,我可能会遇到错误。但我看不出我的代码有什么问题。这是一种独特的方法,所以没有找到太多。
【解决方案2】:

感谢@nathanpearson,我能够在

中找到错误
int* variablesizedarr(int size){
    int* arr= new int [size];
    for(int i=1;i<size;i++){
        cin >>arr[i];
    }
    /*I believe the problem is when I return the arr array*/
    return arr;
    
}

这里 i 的初始化应该是 0,而不是 1。我知道这是一个愚蠢的错误,但是 Hackerrank 调试器在解释错误方面非常糟糕,所以我怀疑我的理解。无论如何,这是不使用向量来制作可变多维数组的另一种方法。

再次感谢 Nathan 抽出宝贵时间理解我的问题。非常感谢。

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多