How to Concatenate Strings

beginner c++11 strings

std::string implements concatentation via the addition operator (+).

#include <string>
#include <iostream>

int main() {
  std::string subject{"She"};
  std::string verb{"is playing"};
  std::string object{"the piano"};

  std::string sentence = subject + " " + verb + " " + object + ".";

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


For more C++ By Example, click here.