C++中的字符串
## C++ 中 `string` 的知识点
### 1. `string` 的基本概念
- `string` 是 C++ 标准库中的一个类,用于表示和操作字符串。
- 需要包含头文件 `<string>`。
- `string` 可以动态调整大小,比 C 风格的字符数组更方便。
#### 样例代码:
```cpp
#include <iostream>
#include <string> // 包含 string 头文件
using namespace std;
int main() {
string str = "Hello, World!"; // 定义一个 string 对象
cout << str << endl; // 输出字符串
return 0;
}
```
---
### 2. `string` 的初始化
- 可以通过多种方式初始化 `string` 对象。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1; // 空字符串
string s2 = "Hello"; // 直接赋值
string s3("C++"); // 使用构造函数初始化
string s4(s2); // 使用另一个 string 对象初始化
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
return 0;
}
```
---
### 3. `string` 的常用操作
#### (1) 获取字符串长度
- 使用 `length()` 或 `size()` 方法。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
cout << "Length: " << str.length() << endl; // 输出 5
cout << "Size: " << str.size() << endl; // 输出 5
return 0;
}
```
#### (2) 访问字符
- 使用下标 `[]` 或 `at()` 方法。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello";
cout << "First character: " << str[0] << endl; // 输出 H
cout << "Second character: " << str.at(1) << endl; // 输出 e
return 0;
}
```
#### (3) 字符串拼接
- 使用 `+` 运算符或 `append()` 方法。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2; // 使用 + 拼接
s1.append(" C++"); // 使用 append 拼接
cout << s3 << endl; // 输出 Hello World
cout << s1 << endl; // 输出 Hello C++
return 0;
}
```
#### (4) 字符串比较
- 使用 `==`, `!=`, `>`, `<` 等运算符。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "apple";
string s2 = "banana";
if (s1 == s2) {
cout << "s1 and s2 are equal" << endl;
} else {
cout << "s1 and s2 are not equal" << endl;
}
if (s1 < s2) {
cout << "s1 is less than s2" << endl;
}
return 0;
}
```
#### (5) 子字符串
- 使用 `substr()` 方法。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
string sub = str.substr(6, 5); // 从第6个字符开始,取5个字符
cout << sub << endl; // 输出 World
return 0;
}
```
#### (6) 查找字符或子字符串
- 使用 `find()` 方法。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "Hello World";
size_t pos = str.find("World"); // 查找子字符串
if (pos != string::npos) {
cout << "Found at position: " << pos << endl; // 输出 6
} else {
cout << "Not found" << endl;
}
return 0;
}
```
---
### 4. `string` 的输入输出
- 使用 `cin` 和 `cout` 进行输入输出。
- 注意:`cin` 会以空格为分隔符,如果需要输入整行,可以使用 `getline()`。
#### 样例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
getline(cin, name); // 读取整行
cout << "Hello, " << name << "!" << endl;
return 0;
}
```
---
### 5. 考前建议
1. **熟悉常用操作**:确保掌握 `length()`、`substr()`、`find()`、`append()` 等常用方法。
2. **多练习代码**:通过编写小程序巩固知识点,例如字符串拼接、查找、比较等。
3. **注意边界条件**:例如空字符串、查找失败等情况。
4. **调试技巧**:学会使用 `cout` 输出中间结果,帮助理解程序运行过程。
5. **时间管理**:考试时先做有把握的题目,遇到难题先跳过,最后再回来解决。
浙公网安备 33010602011771号