I want to find the most efficient way to concatenate multiple strings of type std::string.
One of the issues is that I also have a char within it, and another issue is that I need it to be as fast as possible. Let's say I have the following variables defined:
std::string defaultName = "default";
std::string paramFullName = "Elasticity";
First try:
std::string paramName = defaultName + "_" + paramFullName[0] + "_";
This code does not compile with Intel C++14 compiler.
Second try:
std:string paramName;
paramName += defaultName + "_";
paramName += paramFullName[0];
paramName += "_";
This time the output came as expected:
"default_E_"
I still wanted to test another way:
Third try:
std:string paramName;
paramName.append(defaultName + "_");
paramName.append(1, paramFullName[0]);
paramName.append("_");
Output was OK again, because of the separation of the char from the strings.
"default_E_"
When testing for timing, I've found out that the append option is faster that the +=.
I want to know if there is a better, more efficient way,
and also could I minimize the number of lines so it wouldn't look so ugly?
#include <string>and also spelldefaulNamecorrectly and/or remember the;at the end ofstd::string paramFullName = "Elasticity"?appendand other concatenation methods could require more memory and allocate new memory in each step.defaulName + "_" + paramFullName[0] + "_"should be parsed as((defaulName + "_") + paramFullName[0]) + "_". I.e. all "additions" should be between either astd::stringand aconst char*, or between astd::stringand achar, which both should work fine.std::stringstreamwas much slower than anything else.