C ++-3-d数据结构-我应该使用指针向量还是向量向量? [英] C++ - A 3-d data structure - should I use vector of pointers or vector of vectors?

查看:93
本文介绍了C ++-3-d数据结构-我应该使用指针向量还是向量向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须维护一个3维的数据结构.因此,让我们说它的尺寸为:-l x m x n.自从构建数据结构以来,在我希望编写l和m的程序中就已经知道了.但是n必须在整个运行时都是动态的.对于网格lxm中的不同向量,n可能不同.(一旦创建了此结构,就永远不会破坏它,因为我将一直需要它.)

I have to maintain a data structure which is 3 dimensional. So let us say it's dimensions are :- l x m x n. In the program that I wish to write l and m will be known since the time the data structure is being constructed . But n has to be dynamic throughout runtime. And n may be different for the different vectors in the grid lxm. (Once this structure is created I never intend to destruct it as I will be needing it throughout).

目前,让我们假设它是我要制作的二维数据结构,我应该制作vector >还是vector< vector< int> *>?我也知道在第一种情况下如何将向量初始化为所需的大小,即我可能会做类似:-

For the moment let us assume it's a 2-dimensional data structure that I wish to make, should I make a vector<vector<int>> or a vector<vector<int>*> ? Also I know how I might initialise the vector to the required size in the first case, i.e I might do something like :-

vector<vector<int> > A(m)

将外部尺寸的大小初始化为m.但是在第二种情况下,一旦创建了指向矢量的指针的矢量,就如何创建指针指向的矢量.

to initialise the size of the outer dimension to m. But in the second case, once I have created a vector of pointers to vectors, how do I create the vectors to which the pointers point.

将其用于3 d情况下,我应该使用,向量<向量<向量>或向量<向量<向量<向量* >>>或其他组合?

Carrying this to the 3-d case, should I use , a vector<vector<vector>> or a vector<vector<vector*>> or some other combination ?

请提出任何建议,以便在问题未正确定格的情况下重新提出问题.

Please, suggest any changes so that I may reframe the question if it's not framed properly.

推荐答案

最好使用单个 vector (而不是嵌套的),因为在这种情况下,保证内存是连续的而且由于没有缓存未命中,因此您的代码将更快.在这种情况下,您需要从3D(2D)映射到1D,反之亦然,但这很简单

You're better using a single vector (and not nested ones), since the memory is guaranteed to be contiguous in this case and your code will be way faster due to no cache misses. In this case, you need to map from 3D (2D) to 1D and vice-versa, but this is pretty straightforward

用于2D:

(x,y) <-> y*DIM_X + x;

对于3D:

(x,y,z) <-> z*(DIM_Y*DIM_X) + y*DIM_X + x;

如果您真的坚持使用嵌套向量,则可以执行以下操作:

If you really insist to use nested vectors, you can do something like:

#include <vector>

template<typename T>
using vec = std::vector<T>; // to save some typing

int main()
{
    // creates a 5 x 4 x 3 vector of vector of vector
    vec<vec<vec<double>>> v{5, vec<vec<double>>{4, vec<double>{3}}};
}

编辑

响应您的最新使用

std::vector<std::vector<double>> v{DIM_X*DIM_Y, std::vector<double>};
// address (x,y,z)
v[y*DIM_X+x, z] = val_x_y_z;

如果您进一步碰巧知道内部向量的尺寸,则可以使用 std :: vector :: reserve .由于不会进行任何(缓慢的)重新分配,因此可以加快处理速度.

If you further happen to know the dimensions of the inner vectors, you can preallocate memory for them using std::vector::reserve. This will speed things up since there won't be any (slow) re-allocations.

这篇关于C ++-3-d数据结构-我应该使用指针向量还是向量向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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