How to add many strings in c++

user188276 picture user188276 · Feb 20, 2010 · Viewed 63.4k times · Source

As I know that C++ only allows to add 2 strings together, i.e: s = s1 + s2

But how can I add many strings together? Like:

s = s1 + s2 + s3 + s4 + ... + sn

Answer

cpx picture cpx · Feb 20, 2010

If you're trying to append string objects of std::string class, this should work.

string s1 = "string1";
string s2 = "string2";
string s3 = "string3";

string s = s1 + s2 + s3;

OR

string s = string("s1") + string("s2") + string("s3") ...