C语言和C++语言中 string类详解 联系客服

发布时间 : 星期日 文章C语言和C++语言中 string类详解更新完毕开始阅读e63ff754ff4733687e21af45b307e87101f6f86a

有了 string 类,我们可以使用+或+=运算符来直接拼接字符串,非常方便,再也不需要使用C语言中的 strcat()、strcpy()、malloc() 等函数来拼接字符串了,再也不用担心空间不够会溢出了。

用+来拼接字符串时,运算符的两边可以都是 string 字符串,也可以是一个 string 字符串和一个C风格的字符串,还可以是一个 string 字符串和一个字符数组,或者是一个 string 字符串和一个单独的字符。请看下面的例子:

#include #include using namespace std;

int main(){

string s1 = \; string s2 = \; char *s3 = \; char s4[] = \; char ch = '@';

string s5 = s1 + s2; string s6 = s1 + s3; string s7 = s1 + s4; string s8 = s1 + ch;

cout<

return 0; }

运行结果: