Range-for-loops and std::vector

前端 未结 3 1668
我在风中等你
我在风中等你 2020-11-28 10:16

Why does this code work

std::vector intVector(10);
for(auto& i : intVector)
    std::cout << i;

And this doesn\'t?

<
3条回答
  •  再見小時候
    2020-11-28 11:09

    vector is (usually) specialized explicitly to store each bool in a single bit, reducing the storage costs from one byte per value to one byte per eight values. No processor I know of offhand is bit addressable, so it's impossible to store a reference to the values in the vector. You need to use plain auto, not auto& for the iteration value i.

提交回复
热议问题