多维数组与任意边界类成员 [英] Multidimensional arrays as class member with arbitrary bounds

查看:150
本文介绍了多维数组与任意边界类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是重复<一个href=\"http://stackoverflow.com/questions/22975215/c-multidimensional-array-initialization-in-constructor\">C++:在构造多维数组初始化,因为所有的答案显示的承担范围在编译时已知的。

Not a duplicate of C++: multidimensional array initialization in constructor since the answers all appear to assume the bounds are known at compile-time.

我在做一个加权无向图类 arraygraph ,由一个二维数组支持 INT ,由的名字边[] [] 。在实例化时我真的不关心什么<​​code>边缘[] [] 持有; arraygraph 有读取指定的文件名图形的方法和边缘设置为新的INT [n] [N] (其中 N 是文件中的节点#)将其填充它之前的功能。

I'm making a weighted undirected graph class arraygraph, backed by a 2D array of int, by the name of edges[][]. At instantiation time I don't really care what edges[][] holds; arraygraph has a method that reads a graph from a given filename and edges is set to a new int[n][n] (where n is the # of nodes in the file) by that function before it populates it.

麻烦的是,G ++似乎不喜欢我定义了边缘[] [] 的方式。它希望为阵列设置界限,在编译的时候我不知道的知道的界限。我是不是应该重新定义边缘为int * ?或者如边缘[] [0] ?或别的东西完全?

Trouble is, g++ doesn't seem to like the way I've defined edges[][]. It wants to have set bounds for the array, and at compile time I don't know the bounds. Should I just redefine edges as an int *? Or as edges[][0]? Or something else entirely?

我不是一个C ++专家以任何方式(我是一个Python还挺家伙)那么复杂,重量级的选项,如在的未定义大小类成员是有点超出范围阵列(肯定有比一个更简单的方法的的...)。如果我想要做的是一个错误的事情完全不是这也是知道的有用的东西,它会是很方便的知道我该做的吧。

I'm not a C++ expert by any means (I'm a Python kinda guy) so complex, heavyweight options like the ones in Array with undefined size as Class-member are kinda out of scope (surely there's a simpler way than that...). If what I'm trying to do is a wrong thing entirely than that's also a useful thing to know, and it'd be handy to know what I ought to be doing instead.

推荐答案

C ++不知道变长数组。所以,你需要有一定的大小定义数组。这是不可能要么重新定义数组。

C++ doesn't know variable length arrays. So you need to define your array with a constant size. It's not possible either to redefine an array.

您的多维数组有两个选项:

Two options for your multidimensional array:


  • 阵列的动态数组,如 INT实施**边缘

  • 的std ::矢量,而不是又名矢量&lt;矢量&lt;&INT GT;&GT;边缘;

  • dynamic array of arrays, implemented as int **edges
  • std::vector instead aka vector<vector<int>> edges;

载体,如果你需要复制的数据(在一个单一的语句来完成),或改变大小非常方便。所以我建议在第二个选项:

vectors are extremely handy if you need to copy your data (done in a single statement), or change the sizes. So I'd recommend the second option :

int N=10; // dynamic! 
vector<vector<int>> m(N, vector<int>(N));

使用指针的变质是:

The alterative for using the pointer would be:

int N=10; // dynamic! 
int**m = new int*[N];    // allocate the first array 
for (int i = 0; i < N; i++) {  // allocate the second arrays
    m[i] = new int[N]{};
}

在这两种情况下,你会用同样的语法访问数据:

In both case, you'd access the data with the same syntax:

for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++)
        cout << m[i][j] << "\t";
    cout << endl;
}

这篇关于多维数组与任意边界类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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