C++ inserting unique_ptr in map

前端 未结 3 878
醉梦人生
醉梦人生 2020-12-01 12:00

I have a C++ object of type ObjectArray

typedef map> ObjectArray;

What is the syn

3条回答
  •  清歌不尽
    2020-12-01 12:29

    In addition to previous answers, I wanted to point out that there is also a method emplace (it's convenient when you cannot/don't want to make a copy), so you can write it like this:

    ObjectArray object_array;
    auto pointer = std::make_unique(...);  // since C++14
    object_array.emplace(239LL, std::move(pointer));
    // You can also inline unique pointer:
    object_array.emplace(30LL, std::make_unique(...));
    

提交回复
热议问题