Range-for-loops and std::vector

前端 未结 3 1657
我在风中等你
我在风中等你 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:02

    Because std::vector<bool> is not a container !

    std::vector<T>'s iterators usually dereference to a T&, which you can bind to your own auto&.

    std::vector<bool>, however, packs its bools together inside integers, so you need a proxy to do the bit-masking when accessing them. Thus, its iterators return a Proxy.
    And since the returned Proxy is an prvalue (a temporary), it cannot bind to an lvalue reference such as auto&.

    The solution : use auto&&, which will correctly collapse into an lvalue reference if given one, or bind and maintain the temporary alive if it's given a proxy.

    0 讨论(0)
  • 2020-11-28 11:07

    std::vector<bool> does not obey the standard container rules.

    In particular, its operator[] does not return bool&.

    The loop in the invalid code

    #include <vector>
    #include <iostream>
    
    int main() {
      std::vector<bool> boolVector(10);
      for (auto& i: boolVector)
          std::cout << i;
    }
    

    can be rewritten in any of three ways to iterate through the values:

    1. (read-only)

      for (auto i: boolVector)
          std::cout << i;
      
    2. (read-only, possibly inefficient)

      for (auto const& i: boolVector)  
          std::cout << i;
      
    3. (read/write)

      for (auto&& i: boolVector)
          std::cout << i;
      

    The choice between the first and last is down to whether you need to modify the values in the vector, or just to read them.

    0 讨论(0)
  • 2020-11-28 11:09

    vector<bool> 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<bool>. You need to use plain auto, not auto& for the iteration value i.

    0 讨论(0)
提交回复
热议问题