-
May 18th, 2012, 11:57 AM
#1
[RESOLVED] STL string concatenation does not work.
Hello,
Why it's not working and how to make it work?
Code:
string str = "hello" + " *** ";
Thank you.
-
May 18th, 2012, 12:26 PM
#2
Re: STL string concatenation does not work.
Originally Posted by vincegata
Why it's not working and how to make it work?
It's because "hello" and " *** " aren't of the string type. They're const char* and they cannot be added like that.
But fortunately there's a string constructor that takes a const char* so this should work,
Code:
string str = string("hello") + string(" *** ");
Last edited by nuzzle; May 18th, 2012 at 12:30 PM.
-
May 18th, 2012, 12:45 PM
#3
Re: STL string concatenation does not work.
-
May 18th, 2012, 12:51 PM
#4
Re: STL string concatenation does not work.
It's possible to avoid the explicit conversions I suggested. For example you can do this,
Code:
string str = "hello";
str = str + " *** ";
Here instead implicit conversions (from const char* to string) take place behind the scene.
-
May 18th, 2012, 01:00 PM
#5
Re: STL string concatenation does not work.
Originally Posted by nuzzle
It's possible to avoid the explicit conversions I suggested. For example you can do this,
Code:
string str = "hello";
str = str + " *** ";
Here instead implicit conversions (from const char* to string) take place behind the scene.
That's the way I did it but I need to build long strings from pieces so I had to declare several variables.
-
May 18th, 2012, 02:10 PM
#6
Re: [RESOLVED] STL string concatenation does not work.
If you know in advance that the string will become very long, it could make sense to call "reserve" upfront. This way there will be less memory reallocations during the string building process. But this is only important when it has to be quick.
-
May 18th, 2012, 03:02 PM
#7
Re: [RESOLVED] STL string concatenation does not work.
> If you know in advance that the string will become very long, it could make sense to call "reserve" upfront. This way there will be less memory reallocations during the string building process. But this is only important when it has to be quick.
Actually, it does have to be fast, I'll keep "reserve" in mind, I am just not at the stage to optimize the strings -- thx.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|