string memory pre-allocation

J

Jarek

Hi all!

As I understand string class, whenever string is expanded (i.e. by +=
), the memory is reallocated with new bigger size.
Is it makes sense ( from perforamance point of view ) to pre-allocate
some memory for string, i.e.:

string temp(100,' ');

temp.clear();


temp += "a";
temp += "b";
...


Jarek
 
T

Tim Love

Jarek said:
Hi all!

As I understand string class, whenever string is expanded (i.e. by +=
), the memory is reallocated with new bigger size.
Not really. Read about size(), capacity(), and reserve() member functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top