C++ string 类完整学习指南
1. 基础概念与头文件
必需头文件
1 2 3 4 5
| #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cctype>
|
基本声明与初始化
1 2 3 4 5
| string s1; string s2 = "Hello"; string s3(s2); string s4(5, 'A'); string s5 = s2 + " World";
|
2. 输入输出操作
输入方法对比
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| string str;
cin >> str;
getline(cin, str);
int num; string name;
cin >> num; cin.ignore(); getline(cin, name);
|
输出方法
1 2 3 4 5
| string str = "Hello World";
cout << str; cout << str[0]; printf("%s", str.c_str());
|
3. 字符串基本信息获取
长度与容量
1 2 3 4 5 6
| string str = "Hello";
str.length(); str.size(); str.empty(); str.capacity();
|
字符访问
1 2 3 4 5 6 7 8 9 10 11
| string str = "Hello";
char c1 = str[0];
char c2 = str.at(1);
char first = str.front(); char last = str.back();
|
4. 字符串修改操作
追加与连接
1 2 3 4 5
| string str = "Hello";
str.append(" World"); str += "!"; str.push_back('?');
|
插入与删除
1 2 3 4 5 6 7 8
| string str = "Hello World";
str.insert(5, " C++");
str.erase(5, 4); str.clear();
|
替换操作
1 2 3 4
| string str = "Hello World";
str.replace(6, 5, "C++");
|
5. 字符串查找与子串
查找操作
1 2 3 4 5 6 7 8 9 10 11 12
| string str = "Hello World";
size_t pos = str.find("World"); size_t notFound = str.find("Java");
if (pos != string::npos) { cout << "找到子串,位置:" << pos; } else { cout << "未找到子串"; }
|
子串提取
1 2 3 4 5
| string str = "Hello World";
string sub1 = str.substr(6, 5); string sub2 = str.substr(6);
|
6. 字符串比较与遍历
比较操作
1 2 3 4 5 6 7 8
| string s1 = "apple"; string s2 = "banana";
if (s1 == s2) { cout << "相等"; } else if (s1 < s2) { cout << "s1 小于 s2"; }
|
遍历方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| string str = "Hello";
for (int i = 0; i < str.length(); i++) { cout << str[i]; }
for (char c : str) { cout << c; }
for (auto it = str.begin(); it != str.end(); ++it) { cout << *it; }
|
7. 字符串与数值转换
1 2 3 4 5 6 7 8
| int num = 123; string str1 = to_string(num);
string str2 = "456"; int num2 = stoi(str2); double num3 = stod("3.14");
|
8. 常用算法操作
排序与反转
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <algorithm>
string str = "hello";
reverse(str.begin(), str.end());
sort(str.begin(), str.end());
vector<string> words = {"dog", "cat", "ant"}; sort(words.begin(), words.end());
|
字符处理
1 2 3 4 5 6 7 8 9 10 11 12
| #include <cctype>
char c = 'a';
char upper = toupper(c); char lower = tolower('B');
isalpha(c); isdigit(c); isspace(c);
|
9. 实际应用案例
案例1:字符串逆序
1 2 3 4 5 6 7 8 9 10
| reverse(str.begin(), str.end());
int left = 0, right = str.length() - 1; while (left < right) { swap(str[left], str[right]); left++; right--; }
|
案例2:统计字符出现次数和位置
1 2 3 4 5 6 7 8 9 10 11
| string str = "hello world"; char target = 'l'; int count = 0, firstPos = -1;
for (int i = 0; i < str.length(); i++) { if (str[i] == target) { count++; if (firstPos == -1) firstPos = i + 1; } } cout << count << " " << firstPos;
|
案例3:判断回文字符串
1 2 3 4 5 6 7 8
| bool isPalindrome = true; for (int i = 0; i < str.length() / 2; i++) { if (str[i] != str[str.length() - 1 - i]) { isPalindrome = false; break; } } cout << (isPalindrome ? "Yes!" : "No!");
|
案例4:输出单词首字母大写
1 2 3 4 5
| stringstream ss(str); string word; while (ss >> word) { cout << (char)toupper(word[0]); }
|
案例5:寻找第一个不重复字符
1 2 3 4 5 6 7 8 9 10
| int count[256] = {0};
for (char c : str) count[(unsigned char)c]++;
for (int i = 0; i < str.length(); i++) { if (count[(unsigned char)str[i]] == 1) { cout << str[i]; break; } }
|
10. String 特性速查表
| 分类 |
操作 |
语法 |
说明 |
| 基本信息 |
长度获取 |
str.length() 或 str.size() |
获取字符数量 |
|
空判断 |
str.empty() |
是否为空字符串 |
| 字符访问 |
下标访问 |
str[i] |
访问第i个字符 |
|
安全访问 |
str.at(i) |
带边界检查的访问 |
|
首尾字符 |
str.front(), str.back() |
第一个和最后一个字符 |
| 字符串修改 |
追加 |
str.append(s), str += s |
字符串追加 |
|
插入 |
str.insert(pos, s) |
在指定位置插入 |
|
删除 |
str.erase(pos, len) |
删除子串 |
|
替换 |
str.replace(pos, len, s) |
替换子串 |
|
清空 |
str.clear() |
清空字符串 |
| 查找子串 |
查找 |
str.find(s) |
查找子串位置 |
|
提取 |
str.substr(pos, len) |
提取子串 |
| 算法操作 |
反转 |
reverse(str.begin(), str.end()) |
反转字符串 |
|
排序 |
sort(str.begin(), str.end()) |
字典序排序 |
|
交换 |
swap(str1, str2) |
交换两个字符串 |
| 字符处理 |
大小写 |
toupper(c), tolower(c) |
字符大小写转换 |
| 类型转换 |
转字符串 |
to_string(num) |
数字转字符串 |
|
字符串转数字 |
stoi(str), stod(str) |
字符串转数字 |
11. 重要注意事项
getline 后的 cin.ignore
1 2 3 4 5
| int age; string name; cin >> age; cin.ignore(); getline(cin, name);
|
字符串比较不要用 == 比较 C 风格字符串
1 2 3
| string str = "hello"; if (str == "hello") { ... } if (str.c_str() == "hello") { ... }
|
访问前检查边界
1 2 3 4 5 6 7
| string str = "hi";
try { char c = str.at(10); } catch (const out_of_range& e) { cout << "索引越界"; }
|
处理查找失败
1 2 3 4
| string str = "hello"; if (str.find("world") == string::npos) { cout << "未找到"; }
|
wow,你居然看完了,good job,欢迎联系我补充啊!!!
Author:
Lyxy-szs
Permalink:
http://lyxy-szs.github.io/2025/11/20/string/
Slogan:
永远相信美好的事情即将發生?