Using "<<" or "+" to put strings together when using "cout"
Asked Answered
M

3

16

I have seen people output different strings together by using both "<<" and "+".

cout << firstname << lastname << endl;

versus:

cout << firstname + lastname << endl;

Is it better to use "<<" or does it not make much of a difference?

Minded answered 17/1, 2013 at 22:58 Comment(2)
You could also do a test which is faster and post the results.Rhondarhondda
I'm assuming you're referring std::string for operator+ concatenation? This won't work with raw const char *s.Unseal
T
18

Definitely, use << - concatenating the string will create a copy of the two strings pasted together. Whether it also allocates extra memory on top is a matter of how strings are implemented in the C++ library, but if the first and last names are "long enough" (bigger than 8-16 characters together), then it most likely WILL allocate memory (and then free it again when the temporary copy is no longer needed).

The << operator will have very little overhead in comparison, so no doubt it is better.

Of course, unless you do thousands of these things, it's unlikely that you will have a measurable difference. But it's good to not waste CPU cycles, you never know what good use they can be somewhere else... ;)

Tweed answered 17/1, 2013 at 23:10 Comment(1)
See also: premature pessimizationCorrosive
U
20

I would say its better to use << in that particular case. Otherwise, concatenation results in a temporary which could allocate memory for no good reason.

Undershorts answered 17/1, 2013 at 22:59 Comment(0)
T
18

Definitely, use << - concatenating the string will create a copy of the two strings pasted together. Whether it also allocates extra memory on top is a matter of how strings are implemented in the C++ library, but if the first and last names are "long enough" (bigger than 8-16 characters together), then it most likely WILL allocate memory (and then free it again when the temporary copy is no longer needed).

The << operator will have very little overhead in comparison, so no doubt it is better.

Of course, unless you do thousands of these things, it's unlikely that you will have a measurable difference. But it's good to not waste CPU cycles, you never know what good use they can be somewhere else... ;)

Tweed answered 17/1, 2013 at 23:10 Comment(1)
See also: premature pessimizationCorrosive
C
1

Cascading << is a better choice.

For performance, as the other theads mentioned, operator << doesn't necessarily introduce any temporary object. A cascading << can be considered as a pipe.

Also sometimes, you cannot use + if your left-hand operand is not a user-defined type, unless you provide the corresponding operator+. E.g.,

cout << "Hello, " << lastname << endl;// Works
cout << "Hello, " + lastname << endl; // This won't work
Caption answered 17/1, 2013 at 23:20 Comment(3)
For the last line to not work, it would require a really poor implementation of a string type. It will certainly work with std::string.Undershorts
Yes, std::string does have that support.Caption
@K-ballo: Exactly as written, the last line is a const char[], and thus it doesn't support the + operator cout << std::string("Hello, ") + lastname << endl; would workBulgar

© 2022 - 2024 — McMap. All rights reserved.