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

2024-03-22

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

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;
            }
        }
    }
}

我想编写一个将 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.

    }
 };

这样我就可以做这样的事情:

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
}

做这个的最好方式是什么?当矢量变得非常大时,我应该避免哪些常见错误以获得最佳速度?


updated带有可变版本

1. 简单、明确的代码

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

2. 有帮手

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));
    }
};

3. 使用可变参数助手

Because 可变参数是 funadic! http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Variadic-Templates-are-Funadic这是一个使用变量使事情变得更漂亮的版本(取决于品味):

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...) };
    }
};

PS。我稍微编辑了代码以使其更符合标准。模板有一个微妙的问题尾随返回类型 http://en.wikipedia.org/wiki/C%2B%2B11#Alternative_function_syntax指的是同名的重载。 有关此处问题的详细分析,请参阅聊天中的此讨论书签 https://chat.stackoverflow.com/rooms/10/conversation/latereturntype-and-variadics

如果您的编译器不认为它应该工作,请查看它住在 gcc 4.7.2 上 http://liveworkspace.org/code/d72e6a6134dbfe5aebbc32bd06a685c0. 它仅使用 boost 来格式化输出:

. . . .
. . . .
. . . .
. . . .
. . . .
-------
. . . .
. . . .
. . . .
. . . .
. . . .

避免链接腐烂的完整代码:

#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过类构造函数初始化成员变量STL向量 的相关文章

随机推荐