一: C风格字符串连接
#include <iostream>
using namespace std;
int main()
{
const
char *str = "hello ";
const
char *str2 = "world";
const
size_t len = strlen(str)+strlen(str2);
char
*n_str = new char[len+1];
strcpy(n_str,str);
strcat(n_str,str2);
cout<<n_str<<endl;
delete
[] n_str;
return
0;
}
二|:C++ string类型字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
const
string str="hello ";
const
string str2="world";
string
n_str;
n_str
= str;
n_str
+=str2;
cout<<n_str<<endl;
return
0;
}
#include <iostream>
using namespace std;
int main()
{
}
二|:C++ string类型字符串
#include <iostream>
#include <string>
using namespace std;
int main()
{
}
本文介绍了两种不同的字符串连接方式:一种是使用C风格的字符数组通过`strcpy`和`strcat`函数进行连接;另一种是利用C++的`std::string`类实现字符串的拼接。这两种方法分别展示了传统C语言和现代C++处理字符串的不同特点。

591

被折叠的 条评论
为什么被折叠?



