如果我有一个std::vector
ofstd::unique_ptr
并调整其大小,并想按索引添加元素,那么使用它们添加的最佳方法是什么operator=
?
std::vector<std::unique_ptr<item>> _v;
_v.resize(100);
// is it safe to use the assignment operator?
_v[20] = new item;
如果使用的是C ++ 14,则可以使用std :: make_unique
_v[20] = std::make_unique<item>(/* Args */);
否则,如果您使用的是C ++ 14,则可以自己实现std::make_unique
,或使用的构造函数std::unique_ptr
_v[20] = std::unique_ptr<item>(new item(/* Args */));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句