std::vector and std::string reallocation strategy

前端 未结 4 1369
南方客
南方客 2021-01-17 23:03

What is the reallocation strategy used for std::string and std::vector in GCC\'s implementations?

I\'m interested in the specific strategy employed: When I append it

4条回答
  •  一整个雨季
    2021-01-17 23:58

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

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

    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.

提交回复
热议问题