How the vector <string> is stored in memory

I am working on a project where I absolutely need to have continuous data in memory. I want to save a row (maximum 100) (I do not know the actual size of each row). Therefore, I will create a row vector of 100 elements.

std::vector<std::string> vect;
vect.reserve(100)

But the string can be of any size. So how does it work? Is my vector redistributed every time I change the string? Or is it std :: string just like a pointer to the first character of a string, such as char *, for string C?

+4
source share
3 answers
  • Each stringwill be an instance of the class string, and this instance will contain char*.
  • string .
  • string
  • , std::allocator
  • vector shrink_to_fit
  • string string
  • vector ,
  • -, Small String Optimization. , string string , char*
+7

std::vector . std::string , , , . ? , , .

:

class string
{
    T * begin; 
    T * end;
    T * capacity;
}

, 100 100 , POINTS , .

, - std::string, allocator.

, char , + NULL-.

+2

The implementation is stringdetermined by the implementation and is actually changed between different versions of specific compilers (for example, from gcc 4.9 to 5.0). There is absolutely no guarantee that charsequentially stringare contiguous in memory , even if you use a custom allocator .

So, if you really need to charbe contiguous in memory, you should use only vector<char>.

+2
source

Source: https://habr.com/ru/post/1611654/


All Articles