473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 6457
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Axel | last post by:
Hiho, how can I reserve enough memory for a long string? I tried with max_size() but 4GB is to much for my system, an exception ist the result. And I'm getting an exception too when using the...
9
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
8
by: Nader | last post by:
Hello all, In C# string is a reference type but I learned that string is different from other reference types such as class. For example, if you pass a string argument to a method and then...
2
by: Ravichandran Mahalingam | last post by:
Dear Readers, I am trying to construct an XML on the fly based on certain inputs. I have construct the whole request as string and then send it across. I need to have the following: <ARCXML...
1
by: C Downey | last post by:
Whats the difference between Dim recipient() As String and Dim recipient As String() TIA Colleen
2
by: kihoshk | last post by:
I have what I THINK is an incredibly simple question, though I can't resolve it. I have a reference that returns a string which oftentimes contains "\". These returned strings ar produced by a...
3
by: Haibao Tang | last post by:
I have a two-column data file like this 1.1 2.3 2.2 11.1 4.3 1.1 .... Is it possible to substitue all '1.1' to some value else without using re. Thanks.
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.