初始化一个字符串后,是否可以在同一行添加一个char和一个char*:
char mod;//this comes in as a parameter
string line = "text";
line += mod;
line += "more text";
是否有更有效和/或可能更简单的方式来做到这一点?有点像
string line = "text" + mod + "more text";
最佳答案
你的单线行不通因为char *
s 不是字符串,所以你不能使用 +
将它们与 char
连接起来;你只是以一个指针添加结束。如果你想要单线,你可以使用
string line = string("text") + mod + "more text";
但这不会比您的 3 行更有效率。
关于c++ - 如何将 char 和 char* 附加到现有字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14925753/