468,512 Members | 1,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Question about STL string reserve()

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!
Oct 7 '06 #1
2 6157
"Mike Cain" <aa@aa.comwrote:
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.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 7 '06 #2
Mike Cain wrote:
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
Oct 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Axel | last post: by
9 posts views Thread by David Carter-Hitchin | last post: by
8 posts views Thread by Nader | last post: by
2 posts views Thread by Ravichandran Mahalingam | last post: by
1 post views Thread by C Downey | last post: by
2 posts views Thread by kihoshk | last post: by
3 posts views Thread by Haibao Tang | last post: by
8 posts views Thread by John Salerno | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.