+ 1

How do I sum two strings

Today I was trying to sum two strings in c ++ that was for a program that I was coding but I tried to sum the string in two ways and I could not find the correct way to sum them, the forms I used were: 1st form: std :: string a = "Hello"; std :: string b = "world"; std :: string c = b + a; cout << c << endl; 2nd form: std :: string a = "Hello"; std :: string b = "world"; std :: stringstream ex; ex << a << b; cout << ex << endl; But, both ways give the compiler an error. Can anyone tell me an alternative to sum two strings? Please the alternative is not strcat

2nd Jan 2020, 11:54 PM
Demoshell
Demoshell - avatar
1 Odpowiedź
+ 4
You can either use the append method provided for strings: https://en.cppreference.com/w/cpp/string/basic_string/append Or the + operator, since it is overloaded for this purpose (both + and += are overloaded, dependant on which one you plan to use): https://en.cppreference.com/w/cpp/string/basic_string/operator%2B%3D https://en.cppreference.com/w/cpp/string/basic_string/operator%2B
3rd Jan 2020, 12:20 AM
Shadow
Shadow - avatar