Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why is the vector in C++ not a STL container

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report