在C++编程中,#include 是一个预处理指令,用于引入C++标准库中的字符串处理头文件。该头文件定义了std::string类及其相关功能,为开发者提供了高效、安全的字符串操作方式。
#include
#include
using namespace std;
int main() {
string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2;
cout << result << endl; // 输出: Hello World
cout << "字符串长度: " << result.length() << endl;
return 0;
}
| 函数 | 功能描述 |
|---|---|
| length()/size() | 返回字符串长度 |
| append() | 字符串拼接 |
| find() | 查找子字符串 |
| substr() | 获取子字符串 |
| replace() | 替换字符串内容 |
| compare() | 字符串比较 |
#include