I'm interested in the internal implementation of std::string
concatenation in GCC. Specifically, suppose I want to concatenate some relatively large strings, a
and b
. I'm quite wary of string concatenation in general, whereas strings are immutable in a lot of high-level languages.
#include <iostream>
int main(){
std::string a = "This would be some kind of data.";
std::string b = "To be concatenated with this, and other things.";
// Is building c this way equivalent to strcpy'ing a, ' ', b, and '\n' into
// a sufficiently large chunk of memory, or are intermediate variables used
// and discarded?
std::string c = a + ' ' + b + '\n';
std::cout << c;
}
Is building c
this way equivalent to strcpy
'ing a
, ' '
, b
, and '\n'
into a sufficiently large chunk of memory, or are intermediate variables used and discarded?