如何正确初始化对象的 boost multi_array ?

Cor*_*rey 5 c++ boost boost-multi-array

我惊讶地发现boost::multi_array似乎其初始元素的分配方式与std::vector. 它似乎没有用唯一的元素填充每个元素(使用其默认值或默认构造函数)。我无法找到有关此的更多信息。

有没有办法multi_array在每个元素上使用唯一的对象来填充本身?

例如,请考虑以下情况:

static int num = 0;

struct A {
   int n;
   A() : n((::num)++) {
      std::cout << "A()" << std::endl;
   }
   virtual ~A() {}

   void print() {
      std::cout << "n=" << n << std::endl;
   }
};

int main() {
   std::cout << "vector:" << std::endl;
   std::vector<A> v(3);
   for (auto x : v) {
      x.print();
   }

   std::cout << "multi:" << std::endl;
   boost::multi_array<A, 2> m(boost::extents[2][2]);
   for (auto x : m) {
      for (auto y : x) {
         y.print();
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

这导致输出:

vector:
A()
A()
A()
n=0
n=1
n=2
multi:
A()
n=3
n=3
n=3
n=3
Run Code Online (Sandbox Code Playgroud)

为什么构造函数只被调用一次multi_array?如何multi_array用唯一的对象“填充”(使用 的A默认构造函数)?

seh*_*ehe 4

要快速填充整个数组,请执行类似fill_n\xc2\xb9 的操作:

\n\n
std::fill_n(a.data(), a.num_elements(), 0);\n
Run Code Online (Sandbox Code Playgroud)\n\n

通过 boost,multi_array您可以使用自己的内存缓冲区的视图来获得相同的性能(std::uninitialized_copy是您的朋友)。(实际上,您甚至可以在现有内存上映射数组视图,并且您希望保留现有值)。

\n\n

我在这里写了一个关于此的比较演示:pointers to a class indynamically allocates boost multi_array, not compiling

\n\n

Live On Coliru

\n\n
#include <boost/multi_array.hpp>\n#include <type_traits>\n#include <memory>\n\nstruct octreenode { int a; int b; };\n\nclass world {\npublic:\n    world(double x, double y, double z, int widtheast, int widthnorth, int height)\n            : \n                originx(x), originy(y), originz(z), \n                chunkseast(widtheast), chunksnorth(widthnorth), chunksup(height)\n    {\n#define OPTION 4\n\n#if OPTION == 1\n        static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made");\n        //std::uninitialized_fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72});\n        std::fill_n(chunk.data(), chunk.num_elements(), octreenode {1, 72});\n#elif OPTION == 2\n        for(auto a:chunk) for(auto b:a) for(auto&c:b) c = octreenode{1, 72};\n#elif OPTION == 3\n        for (index cz = 0; cz < chunksnorth; ++cz) {\n            for (index cx = 0; cx < chunkseast; ++cx) {\n                for (index cy = 0; cy < chunksup; ++cy) {\n                    chunk[cz][cx][cy] = octreenode{1, 72};\n                }\n            }\n        }\n#elif OPTION == 4\n        static_assert(std::is_trivially_destructible<octreenode>::value, "assumption made");\n        for (index cz = 0; cz < chunksnorth; ++cz) {\n            for (index cx = 0; cx < chunkseast; ++cx) {\n                for (index cy = 0; cy < chunksup; ++cy) {\n                    new (&chunk[cz][cx][cy]) octreenode{1, 72};\n                }\n            }\n        }\n#endif\n        (void) originx, (void) originy, (void) originz, (void) chunksup, (void) chunkseast, (void) chunksnorth;\n    }\n\nprivate:\n    double originx, originy, originz;\n    int chunkseast, chunksnorth, chunksup;\n\n#if 1\n    typedef boost::multi_array<octreenode, 3> planetchunkarray; // a boost_multi for chunks\n    typedef planetchunkarray::index index;\n    planetchunkarray chunk{boost::extents[chunksnorth][chunkseast][chunksup]};\n#else\n    static_assert(boost::is_trivially_destructible<octreenode>::value, "assumption made");\n\n    std::unique_ptr<octreenode[]> raw { new octreenode[chunksnorth*chunkseast*chunksup] };\n    typedef boost::multi_array_ref<octreenode, 3> planetchunkarray;\n    typedef planetchunkarray::index index;\n    planetchunkarray chunk{raw.get(), boost::extents[chunksnorth][chunkseast][chunksup]};\n#endif\n};\n\nint main() {\n    world w(1,2,3,4,5,6);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

变体 usingmulti_array_ref是如何避免复制构造元素的示例(它类似于将std::vector未初始化的内存用于保留但未使用的元素时使用的优​​化)。

\n\n
\n\n

\xc2\xb9 当然,对于唯一值,请使用std::iotastd::generate

\n\n\n