- C++
C++ string类学习笔记
- 2025-7-24 16:20:46 @
C++ string类学习笔记
一、string类简介
C++标准库中的string
类提供了强大而灵活的字符串处理功能,位于头文件<string>
中。使用string
类前需要包含该头文件,并使用using namespace std;
以避免每次都写std::string
。
#include <iostream>
#include <string>
using namespace std;
二、string对象的创建与初始化
1. 默认构造函数
创建一个空的字符串对象。
string s1; // 创建空字符串
2. 用C风格字符串初始化
string s2 = "Hello"; // 直接初始化
string s3("World"); // 构造函数初始化
3. 用另一个string对象初始化
string s4 = s2; // 拷贝初始化
string s5(s3); // 拷贝构造
4. 用字符重复初始化
string s6(5, 'a'); // 初始化为"aaaaa"
5. 用子串初始化
string s7(s2, 1, 3); // 从s2的位置1开始取3个字符,即"ell"
三、string的基本操作
1. 输入输出
string s;
cin >> s; // 从标准输入读取字符串(遇空格停止)
cout << s; // 输出字符串
getline(cin, s);// 读取一行,包括空格
2. 字符串拼接
string s1 = "Hello";
string s2 = " World";
string s3 = s1 + s2; // 拼接为"Hello World"
s1 += s2; // s1变为"Hello World"
3. 字符串比较
string s1 = "apple";
string s2 = "banana";
if (s1 == s2) {...} // 相等比较
if (s1 < s2) {...} // 字典序比较
4. 字符串长度
string s = "Hello";
int len = s.length(); // 或s.size(),返回5
bool empty = s.empty(); // 判断是否为空
四、string的成员函数详解
1. 访问字符
string s = "Hello";
char c = s[0]; // 下标访问,'H',不检查越界
char c2 = s.at(1); // at()方法访问,'e',检查越界
2. 修改字符串
string s = "Hello";
s[0] = 'J'; // 修改单个字符,变为"Jello"
s.append(" World"); // 追加字符串,变为"Jello World"
s.insert(5, ","); // 在位置5插入逗号,变为"Jello, World"
s.erase(5, 2); // 从位置5开始删除2个字符,变回"JelloWorld"
s.replace(5, 0, ", "); // 从位置5开始替换0个字符为", ",变为"Jello, World"
3. 查找操作
string s = "Hello World";
size_t pos1 = s.find("World"); // 查找子串,返回6
size_t pos2 = s.find('o'); // 查找字符,返回4
size_t pos3 = s.find("World", 7); // 从位置7开始查找,返回string::npos(未找到)
size_t pos4 = s.rfind('o'); // 反向查找,返回7
4. 子串操作
string s = "Hello World";
string sub1 = s.substr(6, 5); // 从位置6开始取5个字符,"World"
string sub2 = s.substr(6); // 从位置6到末尾,"World"
5. 大小写转换
#include <algorithm>
string s = "Hello";
// 转换为小写
transform(s.begin(), s.end(), s.begin(), ::tolower); // "hello"
// 转换为大写
transform(s.begin(), s.end(), s.begin(), ::toupper); // "HELLO"
6. 其他常用函数
string s = " Hello ";
s.trim(); // 移除首尾空格(C++20及以上)
s.clear(); // 清空字符串
s.resize(10, 'x'); // 调整大小为10,不足部分用'x'填充
五、string与C风格字符串的转换
1. string转C风格字符串
string s = "Hello";
const char* cstr = s.c_str(); // 返回const char*
const char* data = s.data(); // C++11起,与c_str()相同
2. C风格字符串转string
const char* cstr = "Hello";
string s = cstr; // 直接赋值
六、示例程序
下面是一个完整的示例程序,演示了string
类的常用操作:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
// 创建字符串
string s1 = "Hello";
string s2("World");
string s3 = s1 + " " + s2; // 拼接字符串
// 输出字符串
cout << "s3: " << s3 << endl;
// 访问和修改字符
s3[0] = 'h';
cout << "Modified s3: " << s3 << endl;
// 字符串长度
cout << "Length of s3: " << s3.length() << endl;
// 查找子串
size_t pos = s3.find("World");
if (pos != string::npos) {
cout << "\"World\" found at position: " << pos << endl;
}
// 子串操作
string sub = s3.substr(6, 5);
cout << "Substring: " << sub << endl;
// 大小写转换
transform(s3.begin(), s3.end(), s3.begin(), ::toupper);
cout << "Uppercase s3: " << s3 << endl;
// C风格字符串转换
const char* cstr = s3.c_str();
cout << "C-style string: " << cstr << endl;
return 0;
}
七、注意事项
string
类会自动管理内存,无需手动释放。- 使用
[]
访问字符时不会检查越界,可能导致程序崩溃;建议使用at()
方法。 - 频繁的插入和删除操作可能影响性能,考虑使用
stringstream
或deque<char>
替代。 string
对象的比较是按字典序进行的。
通过掌握string
类的这些操作和函数,你可以更高效地处理字符串相关的编程任务。
0 条评论
目前还没有评论...