在 C++ 中将 2D 向量作为参数传递给函数的正确方法 [英] Right Way of Passing a 2D vector as an argument to a function in C++

查看:64
本文介绍了在 C++ 中将 2D 向量作为参数传递给函数的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将二维向量作为参数的函数.这编译得很好,但我在执行时遇到分段错误.

I'm trying to write a function that takes a 2D vector as an argument. This compiles just fine, but I get a segmentation fault whilst execution.

//http://www.codechef.com/problems/SUMTRIAN
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int max_sum_path(int maximum_rows,vector<vector<int> >& matrix,int row_index,int colm_index);

int main()
{
  vector<vector<int> > Triangle;
  int num_test_cases;
  cin>>num_test_cases;
  //to iterate over the test cases
  for(int i=0;i<num_test_cases;++i)
  {
    int max_rows;
    cin>>max_rows;
    //to build the 2d vector
    Triangle.resize(max_rows);
    for(int j=0;j<max_rows;++j)
    {
      Triangle[j].resize(j+1);
    }
    //get the input
    for(int j=0;j<max_rows;++j)
    {
      for(int k=0;k<=j;++k)
      {
        cin>>Triangle[j][k];
      }
    }
    cout<<max_sum_path(max_rows,Triangle,0,0)<<endl;
    Triangle.clear();
  }
  return 0;
}

int max_sum_path(int maximum_rows,vector<vector<int> >& matrix,int row_index,int colm_index)
{
  if(row_index >= maximum_rows || colm_index > row_index)
  {
    //we have reached a cell outside the Triangular Matrix
    return 0;
  }
  else
  {
    return matrix[row_index][colm_index] + max(max_sum_path(maximum_rows,matrix,row_index+1,colm_index), max_sum_path(maximum_rows,matrix,row_index+1,colm_index+1));
  }
}

这是我运行时出现的错误.

Here's the error that this gives me when I run it.

    Program received signal SIGSEGV, Segmentation fault.
0x0000000000400e21 in max_sum_path (maximum_rows=3, matrix=..., row_index=3, colm_index=3) at sums_triangle.cpp:49
49          return matrix[row_index][colm_index] + max(max_sum_path(maximum_rows,matrix,row_index+1,colm_index), max_sum_path(maximum_rows,matrix,row_index+1,colm_index+1));

顺便说一下,当我尝试使用二维数组而不是向量时,我遇到了编译时错误.

As a side note, when I tried using a 2D array instead of the vector I got a compile time error.

g++ -Wall sums_triangle.cpp -o sums_triangle
sums_triangle.cpp: In function ‘int main()’:
sums_triangle.cpp:28:46: error: cannot convert ‘int (*)[(((long unsigned int)(((long int)max_rows) + -0x00000000000000001)) + 1)][(((long unsigned int)(((long int)max_rows) + -0x00000000000000001)) + 1)]’ to ‘int**’ for argument ‘2’ to ‘int max_sum_path(int, int**, int, int)’

将多维向量作为参数传递给函数的正确方法是什么.

What is the right way to pass multidimensional vectors as arguments to functions.

推荐答案

您错误地调整了向量的大小.例如:

You're resizing the vectors incorrectly. For example:

for(int j=0;j<max_rows;++j)
{
  Triangle[i].resize(j+1);
}

您在此处多次调整 Triangle[i] 的大小.可能你想说

You are resizing Triangle[i] multiple times here. Probably you meant to say

for(int j=0;j<max_rows;++j)
{
  Triangle[j].resize(j+1);
}

这篇关于在 C++ 中将 2D 向量作为参数传递给函数的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆