c++98字符串拼接
时间: 2024-01-04 13:18:48 浏览: 23
在C++98中,可以使用"+"运算符或append()函数来进行字符串拼接。具体方法如下:
1.使用"+"运算符进行字符串拼接
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello ";
string s2 = "world";
string s3 = s1 + s2;
cout << s3 << endl; // 输出:hello world
return 0;
}
```
2.使用append()函数进行字符串拼接
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello ";
string s2 = "world";
s1.append(s2);
cout << s1 << endl; // 输出:hello world
return 0;
}
```
其中,append()函数还可以指定拼接的起始位置和长度,例如将字符串"abcd"的前两位拼接到s1上:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello ";
string s2 = "abcd";
s1.append(s2, 0, 2);
cout << s1 << endl; // 输出:hello ab
return 0;
}
```