What is the most efficient way to initialize a 3D vector?

后端 未结 5 1344
长情又很酷
长情又很酷 2021-02-03 11:23

I have a 3D string vector in C++:

vector>> some_vector

That I am trying is to find a fast method to all

5条回答
  •  梦如初夏
    2021-02-03 11:36

    I think I'd optimize it by allocating one large block of memory instead of a lot of little ones. This one is only 2D instead of 3D, but gives the basic idea:

    template 
    class matrix { 
        size_t columns_;
        std::vector data;
    public:
        matrix(size_t columns, size_t rows) : columns_(columns), data(columns*rows) {}
    
        T &operator()(size_t column, size_t row) { return data[row*columns_+column]; }
    };
    

    For 3D, you'll need to deal with "planes" (or something) along with rows and columns, but the basic idea is pretty much the same.

提交回复
热议问题