简体   繁体   English

如何在C ++中获取多维向量的维数

[英]How to get dimensions of a multidimensional vector in C++

all 所有

I am using multidimensional STL vector to store my data in C++. 我正在使用多维STL向量将数据存储在C ++中。 What I have is a 3D vector 我拥有的是3D向量

 vector<vector<vector<double>>> vec;

What I want to retrieve from it is : 我想从中检索的是:

 &vec[][1][];    // I need a pointer that points to a 2D matrix located at column 1 in vec

Anyone has any idea to do so? 有人有这样做的想法吗? I would be extremly appreciate any help! 我将非常感谢您的帮助!

Regards 问候

Long

It is best to consider vec just as a vector whose elements happen to be vectors-of-vectors-of-double, rather than as a multi-dimensional structure. 最好将vec视为其元素恰好是双精度矢量的矢量,而不是多维结构。

You probably know, but just in case you don't I'll mention it, that this datatype does not necessarily represent a rectangular cuboid . 您可能知道,但是以防万一,我不愿提及它,该数据类型不一定代表矩形的长方体 vec will only have that "shape" if you ensure that all the vectors are the same size at each level. 如果确保所有向量在每个级别上的大小相同,则vec仅具有该“形状”。 The datatype is quite happy for the vector vec[j] to be a different size from the one at vec[k] and likewise for vec[j][n] to be a vector of different size from vec[j][m] , so that your structure is "jagged". 数据类型非常高兴向量vec[j]大小与vec[k]大小不同,同样,向量vec[j][n]的大小也与vec[j][m]大小不同,这样您的结构就呈“锯齿状”。

So you want to get a pointer to the vector<vector<double>> that is at index 1 in vec . 因此,您想要获得一个指向vector<vector<double>>的指针,该指针位于vec中的索引1处。 You can do that by: 您可以通过以下方式做到这一点:

vector<vector<double>> * pmatrix = &vec[1];

However this pointer will be an unnecessarily awkward means of accessing that vector<vector<double>> . 但是,此指针将是访问vector<vector<double>>的不必要的尴尬手段。 You certainly won't be able to write the like of: 您当然将无法编写以下内容:

double d = pmatrix[j][k];

and expect to get a double at coordinates (j,k) in the "matrix addressed by a pmatrix ". 并期望在“由pmatrix寻址的矩阵”中的坐标(j,k)处获得double pmatrix Because pmatrix is a pointer-to-a-vector-of-vector-of-double; 因为pmatrix是指向double向量的指针; so what pmatrix[j] refers to is the vector-of-vector-of-double (not vector-of-double) at index j from pmatrix , where the index goes in steps of sizeof(vector<vector<double>>) . 所以pmatrix[j]所指的是pmatrix索引j的double-vector-of-double (不是double-of-vector ),其中索引以sizeof(vector<vector<double>>)步长前进。 The statement will reference who-knows-what memory and very likely crash your program. 该语句将引用谁知道什么内存,很可能会使您的程序崩溃。

Instead, you must write the like of: 相反,您必须编写如下内容:

double d = (*pmatrix)[j][k];

where (*pmatrix) gives you the vector-of-vector-of-double addressed by pmatrix , or equivalently but more confusingly: 其中, (*pmatrix)为您提供了矢量的矢量-的双所要解决 pmatrix ,或等效但更容易混淆:

double d = pmatrix[0][j][k];

Much simpler - and therefore, the natural C++ way - is to take a reference , rather than pointer, to the vector<vector<double>> at index 1 in vec . 更简单的方法(因此也是C ++的自然方法)是对vec中索引1处的vector<vector<double>>进行引用而不是指针。 You do that simply by: 您只需通过以下方法即可:

vector<vector<double>> & matrix = vec[1];

Now matrix is simply another name for the vector<vector<double>> at index 1 in vec , and you can handle it matrix-wise just as you'd expect (always assuming you have made sure it is a matrix, and not a jagged array). 现在matrix只是vec中索引1处vector<vector<double>> 另一个名称 ,您可以按期望的方式对矩阵进行处理(始终假设已确保它矩阵,而不是锯齿状阵列)。

Another thing to consider was raised in a comment by manu343726. manu343726在评论中提出了要考虑的另一件事。 Do you want the code that receives this reference to vec[1] to be able to use it to modify the contents of vec[1] - which would include changing its size or the size of any of the vector<double> s within it? 您是否希望接收到对vec[1]引用的代码能够使用它来修改vec[1]的内容-这将包括更改其大小或其中的任何vector<double>的大小?

If you allow modification, that's fine. 如果您允许修改,那很好。 If you don't then you want to get a const reference . 如果不这样做,那么您想获取一个const引用 You can do that by: 您可以通过以下方式做到这一点:

vector<vector<double> > const & matrix = vec[1];

Possibly, you want the receiving code to be able to modify the double s but not the sizes of the vectors that contain them? 可能,您希望接收代码能够修改double但不能修改包含它们的向量的大小吗? In that case, std::vector is the wrong container type for your application. 在这种情况下, std::vector是您的应用程序错误的容器类型。 If that's your position I can update this answer to offer alternative containers. 如果您的位置是我,我可以更新此答案以提供其他容器。

Consider using matrix from some linear algebra library. 考虑使用一些线性代数库中的矩阵。 There are some directions here 有一些方向在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM