Why is the vector in C++ not a STL container
This article mainly explains "why vector in C++ is not STL container", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "why vector in C++ is not STL container"!
preface
The cause is like this. Yesterday, when looking up C++11 range base for loop related things, I saw vector
< bool >It is a proxy iterator, very special, so I studied it curiously.
First vector.
< bool>It is not a vector container in the usual sense. This stems from historical legacy problems.
As early as C++98, there was a vector.
< bool>This type was changed, but because of the space-saving idea at that time, vector
< bool>It's not a byte stored in one byte, it's a bit stored in one bit!
Because there is no direct operation to a bit, so when using operator[], the normal container should return a reference to the corresponding element, but for vector
< bool>Instead of a "true reference," a "proxy reference" is actually accessed, and a "std::vector" is returned.
< bool>:reference"type.
And under normal circumstances,
vector c{ false, true, false, true, false };bool b = c[0];auto d = c[0];
It implies an implicit type conversion for the initialization of b. And for d, its type is not bool, but a vector.
< bool>An internal class in.
And if you change the value of d, the value of c changes as well.
d = true;for(auto i:c) cout