通过二维数组在C函数++ [英] pass 2d array to a function in c++

查看:114
本文介绍了通过二维数组在C函数++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个二维数组传递给一个函数在C ++中。的问题是,它的尺寸是不通用常数。我把尺寸从用户的输入,然后尝试通过阵列。这里就是我doind:

I am trying to pass a 2d array to a function in c++. The problem is that its dimension is not universal constant. I take the dimension as an input from the user and then try to pass the array. Here is what i am doind:

/*
 * boy.cpp
 *
 *  Created on: 05-Oct-2014
 *      Author: pranjal
 */
#include<iostream>
#include<cstdlib>
using namespace std;


class Queue{
private:
    int array[1000];
    int front=0,rear=0;
public:
    void enqueue(int data){
        if(front!=(rear+1)%1000){
            array[rear++]=data;
        }
    }
    int dequeue(){
        return array[front++];
    }
    bool isEmpty(){
        if(front==rear)
            return true;
        else
            return false;
    }
};

class Graph{
public:
    void input(int matrix[][],int num_h){ //this is where I am passing the matrix
        int distance;
        char ans;

        for(int i=0;i<num_h;i++){
            for(int j=0;j<num_h;j++)
                matrix[i][j]=0;
        }
        for(int i=0;i<num_h;i++){
            for(int j=i+1;j<num_h;j++){
                cout<<"Is there route between houses "<<i<<" and "<<j<<": ";
                cin>>ans;
                if(ans=='y'){
                    cout<<"Enter the distance: ";
                    cin>>distance;
                    matrix[i][j]=matrix[j][i]=distance;
                }
            }
        }
        cout<<"The matrix is: \n";
        for(int i=0;i<num_h;i++){
            cout<<"\n";
            for(int j=0;j<num_h;j++)
                cout<<matrix[i][j]<<"\t";
        }
    }
};

int main(){
    Graph g;
    int num_h;
    cout<<"Enter the number of houses: ";
    cin>>num_h;
    int matrix[num_h][num_h];
    g.input(matrix,num_h); //this is where I get an error saying
                           // Invalid arguments ' Candidates are: void input(int (*)[], 
                           // int) '
    return 0;
}

帮助多少AP preciated。谢谢你。

Help much appreciated. Thank you.

推荐答案

在您的code问题:

问题1

void input(int matrix[][],int num_h){

是无效的C ++。在一个多维阵列中,除第一次外尺寸必须常数。一个有效的声明是:

is not valid C++. In a multi-dimensional array, all but the first dimension must be constants. A valid declaration would be:

// Define a constant at the start of the file.
const int MATRIX_SIZE 200;


void input(int matrix[][MATRIX_SIZE],int num_h){

问题2

int matrix[num_h][num_h];

是无效的C ++。 VLA是不是在C ++的支持。

is not valid C++. VLA are not supported in C++.

建议的解决方案

使用的std ::矢量&lt;的std ::矢量&lt;&INT GT;方式&gt; 来捕捉二维数组

修改

void input(int matrix[][],int num_h){

void input(std::vector<std::vector<int>>& matrix){
// You can get the size by calling matrix.size()
// There is no need to pass num_h as an argument.

改变调用code为:

Change the calling code to:

int main(){
    Graph g;
    int num_h;
    cout<<"Enter the number of houses: ";
    cin>>num_h;

    // Construct a 2D array of size num_h x num_h using std::vector
    std::vector<std::vector<int>> matrix(num_h, std::vector<int>(num_h));

    g.input(matrix);
    return 0;
}

这篇关于通过二维数组在C函数++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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