In C++, is it a bad thing to use + for string concatenation? For example below,
string str = "";
int n = 10;
for (int i = 0; i < n; i++) {
str += i + '0';
}
what is the time complexity for this code snippet? Is it O(n)? Does the string + operator in C++ like a vector's push_back and it dynamically grow itself if necessary when adding an item at the end so that the avg time is constant?
Update: One more question: if I need to append char to string but without knowing what is the length ahead, what is the best way to do it if it is not using + operator? I know in Java, we have stringbuilder, do we have similar thing c++ std?