【问题标题】:using nested std::array to create an multidimensional array without knowing dimensions or extents until runtime使用嵌套的 std::array 创建多维数组,直到运行时才知道维度或范围
【发布时间】:2013-12-11 00:33:25
【问题描述】:

一个类是否可能有一个成员是一个多维数组,其维度和范围直到运行时才知道?

我找到了(通过this guide)一种创建结构的方法,以便在编译时使用模板元编程轻松嵌套 std::arrays:

#include <array>
/*
this struct allows for the creation of an n-dimensional array type
*/
template <typename T,size_t CurrentDimExtent,size_t... NextDimExtent>
struct MultiDimArray{
public:
//define the type name nestedType to be a recursive template definition.
  using nestedType=typename MultiDimArray<T,NextDimExtent...>::type;
  using type=std::array<nestedType,CurrentDimExtent>;
};
/*
This struct is the template specialization which handles the base case of the
 final dimensional extent
*/
template <typename T,size_t DimExtent>
struct MultiDimArray<T,DimExtent>{
  using type=std::array<T,DimExtent>;
};

这仍然不能满足我的两个方面的要求(据我所知):

  1. 为了声明这种类型的变量(或指向变量的指针),您必须说明尺寸。
  2. 这仅适用于 DimExtent 是常量表达式(在编译时设置)。

为了说明为什么数字 2 是一个明显的问题,这里有一个具有固定维数 (2) 的类,它使用 void* 来引用多维数组:

template <typename T>
class TwoDimGrid{
public:
TwoDimGrid(const size_t extent1,const size_t extent2):
 _twoDimArray(new MultiDimArray<T,extent1,extent2>);
private:
void* _twoDimArray;
};

这不会编译,因为 extent1 和 extent2 不是常量表达式。

其他说明:

  • 我想看看是否可以使用 std:array 来完成,而不是使用原生数组或像 std::vector 这样的动态调整大小的容器。
  • 请在适当的地方使用智能指针(我没有,因为我不太确定如何处理智能 void 指针)。

编辑

我陷入了The XY Problem 的陷阱,X 是这个问题的第一句话,Y 是如何用 std::array 完成它。因此,我创建了一个 new question 并将其留在这里,以防有可能解决 Y 问题。

【问题讨论】:

  • std::array 需要在编译时知道它的大小,你不能让它在运行时推断它,使用 std::vector
  • @user814628 即使使用std::vector,我仍然会遇到编译时必须知道维数的问题。
  • 你可以使用 boost::multi_array 吗?
  • @user814628 哦,是的。这就是我要做的。我仍然很好奇它是如何做到的。我想知道他们是怎么做到的……

标签: c++ templates multidimensional-array template-meta-programming


【解决方案1】:

老派多维数组,类似以下内容:

template <typename T>
class multi
{
   T*myArray;
   size_t x_dim;
   size_t y_dim;
public:
   multi(size_t x, size t y) : x_dim(x), y_dim(y)
   {
        myArray = new T[x*y];
   }

   T& get(int x, int y)
   {
       return myArray[x*y_dim+y];
   }
};

【讨论】:

  • 此代码不创建多维数组,而是模拟二维数组。多维数组需要未定义数量的参数:T& get(x1,x2,x3,.....,xN)。
【解决方案2】:
template <typename T>
class vvc
{
    //possible ragged array ..non rigorous approach
    //with management memory cost per element
    //clearly not as efficient as .... linearized access where access index is
    //row size * r + column
    //memory management courtesy of vector
    public:
    std::vector< std::vector<T> > v;
};

int double_vector()
{
    int x1 = 5;
    int x2 = 3;
    std::vector<int> r(x2);
    vvc<int> vv;
    int k = 0;
    for (int i1 = 0; i1 < x1; ++i1)
    {
        for (int i2 = 0; i2 < x2; ++i2)
        {
            k += 1;
            r[i2] = k;
        }
        vv.v.push_back(r);

    }
    //inspect
    cout << vv.v[0][0] << " first " << endl;
    for (auto const & t1 : vv.v)
    {
        for (auto const &t2 : t1 )
        {
            cout << t2 << " ";
        }
        cout << endl;
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多