Compiler hang when initializing large std::arrays

前端 未结 3 1916
清歌不尽
清歌不尽 2021-01-12 16:21

I need to initialize a very large multidimensional std::array of data:

class Thing;

class World
{
public:
    World() : space{nullptr} {};
             


        
3条回答
  •  醉梦人生
    2021-01-12 16:48

    vector>>> space;
    

    and when initializing it:

    for (int i = 0; i < 1000; i++)
    {
       vector>> sp1;
       for (int j = 0; j < 1000; j++)
       {
          vector> sp2;
          for (int k = 0; k < 1000; k++)
             sp2.push_back(make_unique("params", "to", "construct", "thing"));
    
           sp1.push_back(move(sp2));
       }
    
       space.push_back(move(sp1));
    }
    

    It is similar to your approach, but it constructs the vectors in the heap instead of stack.

提交回复
热议问题