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!
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!