xxxxxxxxxx
// appending to string
#include <iostream>
#include <string>
int main ()
{
// easy way
std::string str = "Hello";
std::string str2 = " World";
std::cout << str + str2 << std::endl;
return 0;
}
xxxxxxxxxx
// CPP code to demonstrate append(str)
#include <iostream>
#include <string>
using namespace std;
// Function to demonstrate append()
void appendDemo(string str1, string str2)
{
// Appends str2 in str1
str1.append(str2);
cout << "Using append() : ";
cout << str1 << endl;
}
// Driver code
int main()
{
string str1("Hello World! ");
string str2("GeeksforGeeks");
cout << "Original String : " << str1 << endl;
appendDemo(str1, str2);
return 0;
}
xxxxxxxxxx
#include <sstream>
// ...
std::stringstream ss;
//put arbitrary formatted data into the stream
ss << 4.5 << ", " << 4 << " whatever";
//convert the stream buffer into a string
std::string str = ss.str();