In c-strings we need to allocate reasonable size of memory. To avoid reallocations in string operations, we can use something like Stringbuilder in C# or Java or - in C - just allocate more memory for string. But still it can be a problem if we don't know the memory requirement in advance. Do we have some implemetation like linked list? I mean to allocate list of blocks of memory and method c_str() which creates c-string from its nodes
liststring a(4); // requested block size
a.append("hello ");
a.append("world");
// should create three nodes, 4 bytes allocated for each
// "hell" -> "o wo" -> "rld"
a.c_str(); // "hello world";
Or do we use another approach if we want to avoid reallocations? Please explain if it is bad idea.
O(1)time on average. If the current size of buffer isnand the length of the string is out of buffer, then buffer of2*nsize should be reallocated. If it became less thenn / 4then buffer ofn / 2size should be reallocated.