C++ 字符串处理全攻略:从 C 风格到现代 string 类
在 C++ 编程中,字符串是处理文本数据的核心载体。从传统的 C 风格字符数组到 C++ 标准库的string类,字符串处理技术经历了从底层操作到高层抽象的演进。本文将系统梳理 C++ 字符串的核心知识点,结合大量示例代码,助你掌握从基础操作到高级应用的全流程技巧。
一、C 风格字符串:基于字符数组的底层操作
C 风格字符串本质是char类型的数组,以空字符\0(ASCII 码 0)作为结束标志。尽管 C++ 推荐使用更安全的string类,但理解 C 风格字符串仍是掌握字符串底层逻辑的基础。
知识点 1:初始化与基本操作
代码示例 1:多种初始化方式
cpp
#include <iostream>
using namespace std;
int main() {
// 方式1:直接初始化字符数组
char str1[] = "Hello, C-style!"; // 自动计算长度,包含'\0'
// 方式2:显式指定数组大小
char str2[20] = "C-style string";
// 方式3:字符指针指向字符串常量
const char* str3 = "Read-only string";
cout << "str1: " << str1 << endl;
cout << "str2[0]: " << str2[0] << endl; // 访问首个字符'H'
return 0;
}
代码示例 2:手动遍历与修改
cpp
#include <iostream>
using namespace std;
int main() {
char str[] = "Modify me";
// 遍历并转换为大写(仅演示,实际应使用ctype.h)
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] -= 32; // 小写转大写
}
}
cout << "Modified: " << str << endl; // 输出 "MODIFY ME"
return 0;
}
知识点 2:C 风格字符串函数
需包含头文件<cstring>,注意手动管理缓冲区大小,避免溢出。
代码示例 1:核心操作函数
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char dest[50] = "Hello";
char src[] = ", World!";
strcat(dest, src); // 连接字符串,结果存入dest
cout << "Concatenated: " << dest << endl; // 输出 "Hello, World!"
int len = strlen(dest); // 计算长度(不含'\0')
cout << "Length: " << len << endl; // 输出13
return 0;
}
代码示例 2:安全函数(避免溢出)
cpp
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char dest[10];
// 使用strncpy指定最大复制长度
strncpy(dest, "Long string", sizeof(dest)-1); // 最多复制9个字符
dest[sizeof(dest)-1] = '\0'; // 手动添加结束符
cout << "Safe copy: " << dest << endl; // 输出 "Long stri"
// 使用strncat安全连接
char buffer[20] = "Prefix";
strncat(buffer, "Suffix", sizeof(buffer)-strlen(buffer)-1);
cout << "Safe concatenate: " << buffer << endl; // 输出 "PrefixSuffix"
return 0;
}
二、C++ 标准库 string 类:现代字符串处理方案
string类位于<string>头文件,封装了动态字符串的管理,支持自动内存分配、边界检查和丰富的成员函数。
知识点 1:基础操作与构造函数
代码示例 1:多种构造方式
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "Direct init"; // 直接初始化
string s2(5, 'A'); // 构造5个'A'组成的字符串:"AAAAA"
string s3(s2.begin(), s2.end()-1); // 迭代器构造,截取前4个'A'
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
return 0;
}
代码示例 2:运算符重载与基本操作
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "Hello", b = "World";
string c = a + ", " + b + "!"; // 字符串拼接
cout << "c: " << c << endl; // 输出 "Hello, World!"
// 比较操作(支持字典序)
if (a < b) {
cout << "a is less than b" << endl; // 输出成立
}
// 访问字符(支持下标和at(),at()带越界检查)
cout << "First char: " << c[0] << endl; // 'H'
cout << "Last char: " << c.at(c.length()-1) << endl; // '!'
return 0;
}
知识点 2:成员函数与高级操作
代码示例 1:查找与替换
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "apple, banana, apple";
// 查找子串位置(从索引0开始)
size_t pos = str.find("banana");
if (pos != string::npos) { // npos表示未找到
cout << "Found at: " << pos << endl; // 输出6
}
// 替换所有"apple"为"pear"
size_t start = 0;
while ((start = str.find("apple", start)) != string::npos) {
str.replace(start, 5, "pear"); // 替换从start开始的5个字符
start += 4; // "pear"长度为4,避免重复匹配
}
cout << "Replaced: " << str << endl; // 输出 "pear, banana, pear"
return 0;
}
代码示例 2:子串与迭代器
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string url = "https://blog.csdn.net/article";
// 提取协议部分(从0到"://"之后)
size_t sep = url.find("://");
string protocol = url.substr(0, sep); // 提取0到sep-1的子串
cout << "Protocol: " << protocol << endl; // 输出"https"
// 使用迭代器遍历字符(支持正向/反向迭代)
for (auto it = url.begin(); it != url.end(); ++it) {
if (*it == '/') *it = '_'; // 将'/'替换为'_'(需非const迭代器)
}
cout << "Modified url: " << url << endl; // 输出"https://blog.csdn_net_article"
return 0;
}
三、字符串输入输出:从控制台到字符串流
知识点 1:标准输入输出
代码示例 1:基本输入输出
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name; // 读取单个单词(遇空格/换行停止)
cout << "Hello, " << name << "!" << endl; // 输入"Alice"则输出"Hello, Alice!"
// 读取含空格的整行
string line;
cout << "Enter a line: ";
getline(cin, line); // 读取直到换行符(不包含换行符)
cout << "You entered: " << line << endl;
return 0;
}
代码示例 2:输入验证与边界处理
#include <iostream>
#include <string>
using namespace std;
int main() {
string password;
do {
cout << "Enter password (6-12 characters): ";
getline(cin, password);
if (password.length() < 6 || password.length() > 12) {
cout << "Invalid length! Try again." << endl;
}
} while (password.length() < 6 || password.length() > 12);
cout << "Password accepted." << endl;
return 0;
}
知识点 2:字符串流(stringstream)
用于字符串与数值 / 自定义类型的高效转换,需包含<sstream>。
代码示例 1:数值与字符串互转
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
// 数值转字符串
int num = 123;
string str_num;
ostringstream oss;
oss << num; // 流式输出到字符串
str_num = oss.str();
cout << "num to string: " << str_num << endl; // 输出"123"
// 字符串转数值(含错误处理)
string str_float = "3.14";
istringstream iss(str_float);
double pi;
if (iss >> pi) {
cout << "string to double: " << pi << endl; // 输出3.14
}
return 0;
}
代码示例 2:复杂数据解析
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
struct Data { int id; string name; double score; };
int main() {
string line = "101,Alice,95.5";
istringstream iss(line);
Data d;
char comma; // 用于分隔符
if (iss >> d.id >> comma >> d.name >> comma >> d.score) {
cout << "ID: " << d.id << ", Name: " << d.name
<< ", Score: " << d.score << endl; // 输出解析结果
}
// 分割多个数据
string data = "apple;banana;orange";
vector<string> fruits;
string fruit;
while (getline(iss.str(), fruit, ';')) { // 重置iss需重新构造
fruits.push_back(fruit);
}
return 0;
}
四、高级应用:从算法到最佳实践
知识点 1:字符串算法
代码示例 1:回文判断(不区分大小写)
#include <iostream>
#include <string>
#include <cctype> // 用于字符转换
using namespace std;
bool isPalindrome(const string& s) {
string cleaned;
// 预处理:转为小写并过滤非字母数字
for (char c : s) {
if (isalnum(c)) { // 检查是否为字母或数字
cleaned += tolower(c); // 转为小写
}
}
// 双指针判断回文
int left = 0, right = cleaned.length() - 1;
while (left < right) {
if (cleaned[left++] != cleaned[right--]) {
return false;
}
}
return true;
}
int main() {
cout << boolalpha << isPalindrome("A man, a plan, a canal: Panama") << endl; // 输出true
return 0;
}
代码示例 2:最长公共前缀
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) return "";
string prefix = strs[0];
for (int i = 1; i < strs.size(); i++) {
int j = 0;
while (j < prefix.length() && j < strs[i].length()
&& prefix[j] == strs[i][j]) {
j++;
}
prefix = prefix.substr(0, j); // 截断到公共部分
if (prefix.empty()) break; // 提前终止
}
return prefix;
}
int main() {
vector<string> strs = {"flower", "flow", "flight"};
cout << "Longest prefix: " << longestCommonPrefix(strs) << endl; // 输出"fl"
return 0;
}
知识点 2:最佳实践
- 优先使用 string 类:避免手动管理 C 风格字符串的内存,减少缓冲区溢出风险。
- 善用迭代器与范围 for:现代 C++ 推荐使用for (char c : str)遍历字符串,简洁且安全。
- 性能优化:
-
- 频繁拼接时使用string::append()而非+,减少临时对象创建
-
- 大字符串操作前通过reserve()预分配空间,避免多次重新分配
- 编码处理:处理多字节字符(如中文)时,考虑使用wstring(宽字符)或<codecvt>库(C++17 已弃用,建议使用第三方库如 ICU)。
五、总结
C++ 字符串处理从 C 风格的原始数组操作,发展到string类的安全高效抽象,再到字符串流和算法层面的高级应用,形成了完整的技术体系。掌握不同场景下的字符串处理技巧,不仅能提升代码的健壮性,还能显著提高开发效率。建议在实际项目中优先使用string类,并结合现代 C++ 特性(如范围 for、智能指针)写出更简洁优雅的代码。
如果你在字符串处理中遇到具体问题,欢迎在评论区留言,我们一起探讨解决方案!