【问题标题】:passing a multidimentional array in c++在 C++ 中传递多维数组
【发布时间】:2015-10-06 05:44:13
【问题描述】:
#include <bits/stdc++.h>
using namespace std;

#define HODOR        long long int
#define INF          1234567890
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define dwn(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(c, it)   for( typeof( (c).begin()) it = (c).begin();  it !=  (c).end(); ++it)
#define DWN(c, it)   for( typeof( (c).end()) it = (c).end()-1;  it >= (c).begin(); --it)
#define ss(n)        scanf("%s",n)
#define FILL(x,y)    memset(x,y,sizeof(x))
#define pb           push_back
#define mp           make_pair
#define ALL(v)       v.begin(), v.end()
#define sz(a)        ((int)a.size())
#define SET(v, i)    (v | (1 << i))
#define TEST(v, i)   (v & (1 << i))
#define TOGGLE(v, i) (v ^ (1 << i))
#define gc           getchar
#define pc           putchar


template<typename X> inline void inp(X &n ) {
    register int ch=gc();int sign=1;n=0;
    while( ch < '0' || ch > '9' ){if(ch=='-')sign=-1; ch=gc();}
    while(  ch >= '0' && ch <= '9' ) n = (n<<3)+(n<<1) + ch-'0', ch=gc();
    n=n*sign;
}

inline void inps(char *n) {
    register int ch=gc();
    int sign=1;
    int i=0;
    while( ch != '\n' ){ n[i]=(char)ch; ++i; ch=gc();}
    n[i]='\0';
}

int MaxPath(int arr[][100],int n) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
}

int main() {
    int t,n;
    inp(t);

    while(t--) {
        inp(n);
        int arr[n][n];

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                inp(arr[i][j]);
            }
        }

        float result = MaxPath(arr,n);
   }

   return 0;
}

错误看起来像这样: 错误:无法将参数 '1' 的 'int ()[n]' 转换为 'int ()[100]' 到 ' int MaxPath(int (*)[100], int)'

我在 stckoverflow 上看过很多帖子,但似乎都没有工作

【问题讨论】:

  • 我强烈建议您使用文本编辑器的 sn-ps 功能而不是那些宏。按位的很容易成为函数而不是宏,或者您可以使用std::bitset
  • 代码现在缩进很好
  • 我认为 arr[n][n] 应该是 arr[][100]。编译器必须知道二维数组的一行元素数。在这种特定情况下,您还可以使用指向指针的指针来传递二维数组的地址。
  • 你能不能把它作为一个指针传递给一个指向 int int** arr的指针
  • @bhzag 我试过int** arr 错误:无法转换int (*)[n]’ to ‘int**

标签: c++ arrays parameter-passing


【解决方案1】:

您可以像这样将指针作为指向 int 的指针传入

 int MaxPath(int** arr,int n)

但要使其正常工作,您必须以不同的方式声明 int arr[n][n]

int main() {
    int t,n;
    inp(t);

    while(t--) {
        inp(n);
        int** arr = new int*[n];

        for (int i = 0; i < n; i++)
            arr[i] = new int[n];

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                inp(arr[i][j]);
            }
        }

        float result = MaxPath(arr,n);
   }
   //deallocate arr
   for (int i = 0; i < n; i++)
   {
       delete[] arr[i];
   }

   delete []arr;

   return 0;
}

【讨论】:

    【解决方案2】:

    编译器需要能够在 MaxPath 中计算数据项存储的地址。这只有在它知道矩阵的宽度时才有可能。

    最简单的解决方案之一是创建某种矩阵类,在向量之上实现二维(或多维)类型。它看起来像这样:

    #include <vector>
    #include <stdio.h>
    
    template<class T> class matrix 
    {
       std::vector<T> v;
       int width;
     public:
       matrix(int w, int h): v(w*h),width(w) {}
       T &operator()(int x, int y) { return v[x+y*width]; }
       const T &operator()(int x, int y) const { return v[x+y*width]; }
    
    };
    
    
    void f(matrix<int> &m) 
    {
       printf("%d\n",m(1,2));
    }
    
    int main()
    {
       matrix<int> m(5,5);
    
       m(1,2)=42;
       f(m);
    }
    

    这只是一个小例子,您可能需要为实际应用扩展矩阵类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-30
      • 2012-04-25
      • 2015-03-01
      • 2011-04-20
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多