C++ 通过类构造函数初始化成员变量STL向量

C++ 通过类构造函数初始化成员变量STL向量,c++,stl,C++,Stl,我有以下代码,似乎可以工作: class MapCell { public: int x, y, z; }; void Test3DVector(int size_x, int size_y, int size_z) { vector< vector< vector<MapCell> > > m(size_x, vector<vector<MapCell>>(size_y, vector<MapCell&

我有以下代码,似乎可以工作:

class MapCell
{
    public:
    int x, y, z;
};

void Test3DVector(int size_x, int size_y, int size_z)
{
    vector< vector< vector<MapCell> > > m(size_x, vector<vector<MapCell>>(size_y, vector<MapCell>(size_z)));

    for (int x = 0; x < size_x; ++x)
    {
        for (int y = 0; y < size_y; ++y)
        {
            for (int z = 0; z < size_z; ++z)
            {
                m[x][y][z].x = x;
                m[x][y][z].y = y;
                m[x][y][z].z = z;
            }
        }
    }
}
类映射单元
{
公众:
int x,y,z;
};
void Test3DVector(int size_x,int size_y,int size_z)
{
向量>m(大小x,向量(大小y,向量(大小z));
对于(int x=0;x
我想编写一个类,将3D向量作为成员变量,在构造函数中设置维度,如下所示:

class MyClass
{
    public:
        vector< vector< vector<MapCell>>> m;    // I'm not sure how to write this
    MyClass::MyClass(int size_x, int size_y, int size_z)
    {
        // Do the same initialization as Test3DVector did.

    }
 };
class-MyClass
{
公众:
vector>m;//我不知道怎么写这个
MyClass::MyClass(整数大小x,整数大小y,整数大小z)
{
//执行与Test3DVector相同的初始化。
}
};
这样我就可以做这样的事情:

void SomeFunction()
{
   MyClass grid(5, 5, 5);
   cout << grid->m[1][3][2].x << grid->m[1][3][2].y << grid->m[1][3][2].z << endl;
   // Should output 132
}
void SomeFunction()
{
MyClass网格(5,5,5);

cout m[1][3][2].xm[1][3][2].ym[1][3][2].z使用可变版本更新了

1.简单、明确的代码
完整的代码,以避免链接损坏,因此:

#include <iostream>
#include <vector>
#include <boost/spirit/include/karma.hpp> // for debug output only

using namespace std;

struct MapCell 
{
    friend std::ostream& operator <<(std::ostream& os, MapCell const&)
    { return os << "."; }
};

struct MyClass
{
    vector<vector<vector<MapCell>>> m;
    MyClass(int size_x, int size_y, int size_z)
        : m(vec_matrix(MapCell(), size_z, size_y, size_x))
    { }

private:
   ///////////////////////////////////////////////////
   // variadics are funadic!
   template <typename T> static std::vector<T> vec_matrix(T v, size_t n) 
   {
      return { n, std::move(v) };
   }

   template <typename T, typename... Dim> static auto vec_matrix(T v, size_t n, Dim... other)
      -> std::vector<decltype(vec_matrix(v, other...))> 
   {
      return { n, vec_matrix(v, other...) };
   }
   ////////////
};

int main()
{
    MyClass a(4,5,2);

    using namespace boost::spirit::karma;
    std::cout 
        << format(stream % ' ' % eol % "\n-------\n", a.m) 
        << std::endl;
}
#包括
#包括
#包含//仅用于调试输出
使用名称空间std;
结构映射单元
{
friend std::ostream&operator更新了可变版本,增加了乐趣和荣耀:)PS。我稍微编辑了代码,使其更符合标准。模板存在一个微妙的问题,它涉及同名重载。有关此问题的详细信息,请参阅
class MyClass {
    vector<vector<vector<MapCell>>> m;
public:
    MyClass(int size_x, int size_y, int size_z)
        : m(Dim(size_z, Dim(size_y, Dim(size_x, MapCell())))) {
    }

private:
    template <typename T>
    static std::vector<T> Dim(size_t n, T&& v) {
        return std::vector<T>(n, std::move(v));
    }
};
struct MyClass
{
    vector<vector<vector<MapCell>>> m;
    MyClass(int size_x, int size_y, int size_z)
        : m(vec_matrix(MapCell(), size_z, size_y, size_x))
    { }

private:
    template <typename T> static std::vector<T> vec_matrix(T v, size_t n) {
        return { n, std::move(v) };
    }

    template <typename T, typename... Dim> static auto vec_matrix(T v, size_t n, Dim... other)
        -> std::vector<decltype(vec_matrix(v, other...))> {
        return { n, vec_matrix(v, other...) };
    }
};
. . . .
. . . .
. . . .
. . . .
. . . .
-------
. . . .
. . . .
. . . .
. . . .
. . . .
#include <iostream>
#include <vector>
#include <boost/spirit/include/karma.hpp> // for debug output only

using namespace std;

struct MapCell 
{
    friend std::ostream& operator <<(std::ostream& os, MapCell const&)
    { return os << "."; }
};

struct MyClass
{
    vector<vector<vector<MapCell>>> m;
    MyClass(int size_x, int size_y, int size_z)
        : m(vec_matrix(MapCell(), size_z, size_y, size_x))
    { }

private:
   ///////////////////////////////////////////////////
   // variadics are funadic!
   template <typename T> static std::vector<T> vec_matrix(T v, size_t n) 
   {
      return { n, std::move(v) };
   }

   template <typename T, typename... Dim> static auto vec_matrix(T v, size_t n, Dim... other)
      -> std::vector<decltype(vec_matrix(v, other...))> 
   {
      return { n, vec_matrix(v, other...) };
   }
   ////////////
};

int main()
{
    MyClass a(4,5,2);

    using namespace boost::spirit::karma;
    std::cout 
        << format(stream % ' ' % eol % "\n-------\n", a.m) 
        << std::endl;
}