且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

std :: vector和std :: string重新分配策略

更新时间:2023-11-14 16:19:34

查看中的函数 _M_check_len c> bits / stl_vector.h 。它包含:

Look at the function _M_check_len in bits/stl_vector.h. It contains:

const size_type __len = size() + std::max(size(), __n);

因此,当追加n个元素时,基本策略要么加倍,要么增加n,最大。 pop_back 永不解除分配。 libc ++(LLVM)的作用完全一样。

So the basic strategy when you append n elements is either double the size or increase by n, whichever is largest. pop_back never deallocates. libc++ (LLVM) does exactly the same.

读取代码是唯一能找到字符串,释放等策略的方法。

Reading the code is the only way you'll find out the strategies for string, deallocation, etc.