級聯
你可以使用過載的+和+= 運算子連線 std::strings。使用+運算子:
std::string hello = "Hello";
std::string world = "world";
std::string helloworld = hello + world; // "Helloworld"
使用+= 運算子:
std::string hello = "Hello";
std::string world = "world";
hello += world; // "Helloworld"
你還可以附加 C 字串,包括字串文字:
std::string hello = "Hello";
std::string world = "world";
const char *comma = ", ";
std::string newhelloworld = hello + comma + world + "!"; // "Hello, world!"
你也可以使用 push_back() 來推回個人 chars:
std::string s = "a, b, ";
s.push_back('c'); // "a, b, c"
還有 append(),非常像+=:
std::string app = "test and ";
app.append("test"); // "test and test"