官术网_书友最值得收藏!

There's more...

Internally, the VecDeque is implemented as a ring buffer, also known as a circular buffer. It's called like this because it behaves like a circle: the end touches the beginning.

It works by allocating a continuous block of memory, like the Vec; however, where the Vec always leaves its extra capacity at the end of the block, the VecDeque has nothing against leaving spots inside the block empty. It follows that when you remove the first element, the VecDeque doesn't move all elements to the left, but simply leaves the first spot empty. If you then push an element into the beginning via push_front, it will take the spot freed earlier while leaving the elements after it untouched.

The circular catch in the story is that if you have some capacity in the front of the block but none in the back while using push_back, the VecDeque will simply use that space to allocate the extra element, leading to the following situation:

This is great, because you will not have to worry about this at all while using it, as its iterating methods hide the implementation by always showing you the correct order!

Like the vector, VecDeque will resize itself and move all its elements into a new block when its capacity runs out.

主站蜘蛛池模板: 静宁县| 寿宁县| 平邑县| 千阳县| 张家口市| 泰和县| 宁乡县| 石阡县| 宁夏| 天门市| 安义县| 江口县| 长武县| 罗山县| 太康县| 潼关县| 于都县| 宁化县| 古丈县| 电白县| 文成县| 忻城县| 三明市| 康定县| 会同县| 沂南县| 习水县| 丹东市| 和田县| 平邑县| 德昌县| 嘉兴市| 黄浦区| 柳江县| 左贡县| 巫溪县| 赤壁市| 靖远县| 哈巴河县| 炎陵县| 枝江市|