简体   繁体   中英

Iterating through a unique_ptr of a vector of unique_ptrs

If the vector isn't unique_ptrs or if I don't have a unique_ptr to the vector (and don't dereference) it works, but with both it results in a compile error. I'm not sure what's going on.

auto v = std::make_unique<std::vector<std::unique_ptr<int>>>();
for (auto item : *v)
{

}

You can't copy unique pointers because... well, they are unique .

You should iterate by reference:

for (auto & item : *v)
//       ^^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM