作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果 vector 不是 unique_ptr 或者如果我没有 vector 的 unique_ptr(并且不取消引用)它可以工作,但两者都会导致编译错误。我不确定发生了什么。
auto v = std::make_unique<std::vector<std::unique_ptr<int>>>();
for (auto item : *v)
{
}
最佳答案
您不能复制唯一指针,因为...好吧,它们唯一。
您应该通过引用进行迭代:
for (auto & item : *v)
// ^^^
关于c++ - 遍历 unique_ptr vector 的 unique_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18775638/
我是一名优秀的程序员,十分优秀!