How to Append to Strings

beginner c++11 strings

You can append to std::string with the append method or the addition assignment operator(+=). Both are just as good.

#include <string>
#include <iostream>

int main() {
  std::string sentence{"She"};
  sentence.append(" is playing");
  sentence += " the piano.";

  std::cout << sentence << "\n";
}
She is playing the piano.


For more C++ By Example, click here.