将赋值运算符与unique_ptr向量一起使用

devcodexyz

如果我有一个std::vectorofstd::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;
bl4ckb0ne

如果使用的是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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将“ if let”与逻辑“或”运算符一起使用

将 R `outer` 与 `%in%` 运算符一起使用

将 Dapper 与运算符一起使用

JS vs PHP:与逻辑或运算符一起使用时,赋值运算符优先级

当返回语句时,逗号运算符,braced-init-list和std :: unique_ptr合并在一起

将逗号运算符与条件运算符一起使用

一起使用AND / OR运算符

默认赋值运算符如何与字符串指针一起使用

如何与三元运算符一起使用多重赋值?

类型转换与化合物赋值运算符一起使用

在带有 std::unique_ptr 的类赋值运算符中,

为什么 std::unique_ptr 与赋值运算符不兼容?

C ++赋值与删除的赋值运算符一起工作吗?

std :: transform中的二进制运算符,带有unique_ptr向量

在将抽象基类作为参数的函数中,将指针和赋值运算符与派生类一起使用

移动语义如何与unique_ptr一起使用?

与GLFWwindow一起使用unique_ptr

是否可以将引用运算符与其他运算符一起使用?

将OR运算符与2个相同值的占位符一起使用

如何安全地将unique_ptr与可以返回NULL的函数一起使用?

C ++将删除的函数std :: unique_ptr与基类一起使用

将memcpy与unique_ptr一起使用到矢量

如何将memcpy与unique_ptr一起使用

如何将unique_ptr与更通用的删除器一起使用?

使用sequelize,如何将OR运算符与where条件一起使用?

如何为类 unique_ptr 创建运算符->

为什么unique_ptr运算符*并非例外?

#define 与运算符一起使用

与cout运算符一起使用逻辑OR