Question about STL string reserve()

M

Mike Cain

Two quick questions please:

1) How do I declare a STL string variable that I know in advance I want to
hold 5,000 bytes? I'm using SGI STL with MS VS 6.0 C++ in case that matters.

For example, right now I am doing this:

string myLargeString;
myLargeString.reserve(5000);

But I imagine that is not as efficient to just declaring the variable
upfront so that it has the 5000 bytes reserved.

However I can't figure out how to declare it initially as 5000 bytes. For
instance this does not work:
string myLargeString(5000);
neither does this:
string myLargeString<5000>;

am I missing something?

2) Also on a related note - in this case is it better (efficiency-wise) to
use reserve or resize()?

As a test I did this:
string mystr;
mystr.resize(5000:
mystr.append("hello");

But when I look at mystr it is empty!

However if I use reserve like this:
string mystr;
mystr.reserve(5000):
mystr.append("hello");

then that works and I see "hello" in mystr. So why does this not work when
first using resize instead of reserve?

Thanks!
 
K

Kai-Uwe Bux

Mike said:
Two quick questions please:

1) How do I declare a STL string variable that I know in advance I want to
hold 5,000 bytes? I'm using SGI STL with MS VS 6.0 C++ in case that
matters.

For example, right now I am doing this:

string myLargeString;
myLargeString.reserve(5000);

But I imagine that is not as efficient to just declaring the variable
upfront so that it has the 5000 bytes reserved.

You might be imagining that. To get a realistic assesment, you will have to
measure. I wouldn't be surprised is reserve() + copying is more efficient
than constructing a 5000 character string + copying. The reason is that
reserve can leave the 5000 characters of the string uninitialized.

However I can't figure out how to declare it initially as 5000 bytes. For
instance this does not work:
string myLargeString(5000);
neither does this:
string myLargeString<5000>;

am I missing something?

Try

string myLargeString( 5000, ' ' );
2) Also on a related note - in this case is it better (efficiency-wise) to
use reserve or resize()?

As a test I did this:
string mystr;
mystr.resize(5000:
mystr.append("hello");

But when I look at mystr it is empty!

How did you determine that? In fact, your string should contain 5000
0-characters followed by "hello". Your favorite method of printing strings
is very likely to stop at the first 0-character hiding the remainder of the
string form your eyes.
However if I use reserve like this:
string mystr;
mystr.reserve(5000):
mystr.append("hello");

then that works and I see "hello" in mystr. So why does this not work
when first using resize instead of reserve?

Reserve just allocates the space, resize also fills it. Starting from an
empty string, after reserve(5000) you have still an empty string ut its
internal buffer now has room for 5000 characters; with resize(5000) you get
a string of length 5000 filled with 0-characters.



Best

Kai-Uwe Bux
 
D

Daniel T.

Mike Cain said:
Two quick questions please:

1) How do I declare a STL string variable that I know in advance I want to
hold 5,000 bytes? I'm using SGI STL with MS VS 6.0 C++ in case that matters.

For example, right now I am doing this:

string myLargeString;
myLargeString.reserve(5000);

But I imagine that is not as efficient to just declaring the variable
upfront so that it has the 5000 bytes reserved.

However I can't figure out how to declare it initially as 5000 bytes. For
instance this does not work:
string myLargeString(5000);
neither does this:
string myLargeString<5000>;

am I missing something?

No. What you want isn't available. What you are doing is the right way
to do it. However, with 5000 bytes in the string, you might want to look
into using a std::deque instead, or maybe one of the non-standard string
classes like sgi's rope class.
2) Also on a related note - in this case is it better (efficiency-wise) to
use reserve or resize()?

As a test I did this:
string mystr;
mystr.resize(5000:
mystr.append("hello");

But when I look at mystr it is empty!

It contained 5000 '\0' and then "hello". :)
However if I use reserve like this:
string mystr;
mystr.reserve(5000):
mystr.append("hello");

then that works and I see "hello" in mystr. So why does this not work when
first using resize instead of reserve?

Because resize and reserve do different things. 'reserve' tells the
string to prepare for holding 5000 elements, so it won't have to
allocate more memory until you try putting more than 5000 elements in
it. 'resize' tells it to actually initialize 5000 elements in the string.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top