473,657 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.r eserve(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(5 000);
neither does this:
string myLargeString<5 000>;

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(50 00:
mystr.append("h ello");

But when I look at mystr it is empty!

However if I use reserve like this:
string mystr;
mystr.reserve(5 000):
mystr.append("h ello");

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 6474
"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.r eserve(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(5 000);
neither does this:
string myLargeString<5 000>;

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(50 00:
mystr.append("h ello");

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(5 000):
mystr.append("h ello");

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.r eserve(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(5 000);
neither does this:
string myLargeString<5 000>;

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(50 00:
mystr.append("h ello");

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(5 000):
mystr.append("h ello");

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
5404
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 normal string and putting to much in it. If I want to initialise a std::string using 20MB of memory, how can I do this?
9
8693
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 knowledgeable insight into this. My class Position has a string (private, public - doesn't seem to matter which), which I construct in a member function and wish to return calls from main(). However, although
8
2988
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 buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried resize() passing a smaller size than the
8
5038
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 change the value in that method the modification will not be visible outside the method. However this is not true for classes. In my example I am not using ref keyword. Thanks for feedback.
2
2516
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 version="1.1"> <REQUEST> ..
1
897
by: C Downey | last post by:
Whats the difference between Dim recipient() As String and Dim recipient As String() TIA Colleen
2
1630
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 DLL, which is out of my control. The string is assigned to a variable: string returnedValue; returnedValue=Encrypt("my data"); (returnedValue is assigned something like "x9wk2\nSjsk"; notice the
3
1068
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
2256
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' clue = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.