gpt4 book ai didi

c++ - 遍历 unique_ptr vector 的 unique_ptr

转载 作者:太空狗 更新时间:2023-10-29 19:37:03 27 4
gpt4 key购买 nike

如果 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com