代码之家  ›  专栏  ›  技术社区  ›  Amit G.

分配给一个数组的STD::

  •  -2
  • Amit G.  · 技术社区  · 8 年前
    struct MyStruct
    {
        int x = 0;
    }
    
    std::array<std::unique_ptr<MyStruct>, 10> Arr;
    
    // Arr[0] = ?
    

    将对象赋给这样一个数组的语法是什么? My reference

    2 回复  |  直到 8 年前
        1
  •  1
  •   Amit G.    8 年前

    Arr[0].reset(new MyStruct);
    

    Arr[0] = std::make_unique<MyStruct>(); // Since C++14
    
        2
  •  0
  •   Surt    8 年前

    std::array<std::unique_ptr<MyStruct>, 10> Arr {
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>(),
      std::make_unique<MyStruct>()
    };