Skip to main content
gamedev.net
🔒 Locked

std::string concatenation

Started by gimp Jul 30, 2006 at 4:44 PM 7 replies 2k views
Original Post
gimp
gimp
Maaaany years ago I learnt VB, before c++ in fact. On thing VB does far better than C++ and light years ahead of C is make strings easy to use. In VB I concatenate multiple values together to form a string. I may not have looked hard enough but I never did find a nice way of writing something would look like this: std::string Output = "ERROR [" + Type + "] : " + Message + CRLF; Where Type and Message are std::string's and CRLF is a const char*. Is there a way of doing this that I'm not aware of without creating my own string class and overloading + operator? From what I understand as well the + operator would make a copy of the string from right to left, making the whole process a fair bit slower(Not that I often do this with speed in mind but I'd have to consider it. Right now I do something like this: std::string Output("ERROR ["); Output += Type Output += "] : "; Output += Message; Output += CRLF; Is there a much better way?
Chris Brodie
deathkrush
deathkrush
Check out this article about fast string concatenation. You can use the STLsoft library to define your own string class that doesn't create temporary copies when doing multiple concatenations on a single line.
deathkrushPS3/Xbox360 Graphics Programmer, Mass Media.Completed Projects: Stuntman Ignition (PS3), Saints Row 2 (PS3), Darksiders(PS3, 360)
mikeman
mikeman
Oops, I misinterpreted the question. You are aware that the expression works but you're worried about performace. In that case, listen to AP. Don't waste your time thinking about how you will gain 0.0001ms from string concatenation unless you profile your code and see that this is a bottleneck. But if you're so concerned with performance(which IMO you shouldn't be) you could use this expression:

std::string Output = std::string("ERROR [")+=Type+="] : "+=Message+=CRLF;

if I'm not mistaken, this will create just one temporary string.


[Edited by - mikeman on July 30, 2006 5:08:22 PM]
smart_idiot
smart_idiot
Who cares if it's slightly slower than it could be, it's easy to read and it's not like you're going to be generating thousands of error messages per second.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
CTar
CTar
In case you really find out it's a problem, and you are willing to use your own string type, then you could use expression templates, but only do it if you have a real need for this short of thing. You could also just make a string concentation function with overloads from 1 to n arguments (using Boost.Preprocessor).
Spoonbender
Spoonbender
What makes you think VB's version was faster?
gimp
gimp
Looks like I'm a silly boy. Somewhere along the line I got the idea that + didn;t work with strings. I guess I was trying on concatinate different string types.

Many thanks all. You are all right of course... I probably shouldn't be concerned about the speed of my logging during system startup :)
Chris Brodie

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.