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?
std::vector
. std::string
, , , . ? , , .
:
class string
{
T * begin;
T * end;
T * capacity;
}
, 100 100 , POINTS , .
, - std::string
, allocator.
, char , + NULL-.
The implementation is string
determined 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 char
sequentially string
are contiguous in memory , even if you use a custom allocator .
So, if you really need to char
be contiguous in memory, you should use only vector<char>
.