SoFunction
Updated on 2025-03-05

Tell me about the special nature of vector and why it is not an STL container

Preface

The cause is this. Yesterday, when I was looking for C++11's range base for loop related stuff, I saw that vector<bool> is a proxy iterator, which is very special, so I curiously studied it.

First of all, vector<bool> is not a vector container in the usual sense, it originates from historical legacy issues.

As early as C++98, the type of vector<bool> was found, but because in order to consider the idea of ​​saving space, the vector<bool> was not stored by one Byte, it was stored by one bit and one bit!

Because there is no direct bit to operate, when using operator[], the normal container should return a reference to the corresponding element, but for vector<bool>, it actually accesses a "proxy reference" instead of a "true reference", and returns an object of type "std::vector<bool>:reference".

Under normal circumstances

vector<bool> c{ false, true, false, true, false };
bool b = c[0];
auto d = c[0];

For the initialization of b, it actually implies an implicit type conversion. For d, its type is not bool, but an inner class in a vector<bool>.

If the value of d is modified at this time, the value in c will also be modified as well.

d = true;
for(auto i:c)
  cout&lt;&lt;i&lt;&lt;" ";
cout&lt;&lt;endl;
//The above formula will output1 1 0 1 0

If c is destroyed, d will become a dangled pointer, and then the operation of d is an undefined behavior.

Therefore, it cannot satisfy some basic operations of the container, such as taking the address and pointer initialization operation [because there is no way to get the address for a single bit, or make references]

vector&lt;bool&gt; c{ false, true, false, true, false };
bool &amp;tmp = c[0];  //Error, cannot be compiled, for reference, because c[0] is not an lvaluebool *p = &amp;c[0];  //mistake,Cannot compile,Because a temporary address cannot be bound to a pointer

So why is it said that vector<bool> is not a standard container? It is because it cannot support the basic operations that some containers should have.

What is the correct way of using C++11's range-based for?

Clause 6: When auto deduces unexpected types, use explicit type initialization semantics

Summarize

The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support.