1.string类对象的容量操作
1.1获取容量和大小
apacity()方法返回字符串当前分配的内存空间大小(以字符为单位),不包括终止符\0。size()或length()方法返回字符串中实际存储的字符数量。
cpp
std::string str = "Hello";
std::cout << "Size: " << str.size() << std::endl; // 输出 5
std::cout << "Capacity: " << str.capacity() << std::endl; // 输出可能大于等于5
1.2调整容量
eserve(size_type n)方法用于预分配内存空间,以避免频繁的内存重新分配。如果n大于当前容量,字符串会分配至少n个字符的空间;否则,该方法可能无效果。
cpp
std::string str;
str.reserve(100); // 预分配至少100个字符的空间
1.3清空字符串
clear()方法清空字符串内容,但不会改变其容量。empty()方法用于检查字符串是否为空。
cpp
std::string str = "Hello";
str.clear(); // 清空内容
if (str.empty()) {
std::cout << "String is empty" << std::endl;
}
1.4operator[] 功能说明
用于返回字符串中指定位置 pos 的字符。若为 const string 对象调用,则返回不可修改的字符引用;非 const 对象可修改字符。需确保 pos 在有效范围内(0 <= pos < size()),否则行为未定义。
cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 访问第 0 个字符('H')
char first_char = str[0];
std::cout << "First character: " << first_char << std::endl;
// 修改第 7 个字符('W' -> 'w')
str[7] = 'w';
std::cout << "Modified string: " << str << std::endl;
// 遍历字符串并打印每个字符
for (size_t i = 0; i < str.length(); ++i) {
std::cout << str[i];
}
std::cout << std::endl;
return 0;
}
输出
applescript
First character: H
Modified string: Hello, world!
Hello, world!
1.5begin 和 end 功能说明
begin()返回指向字符串第一个字符的迭代器。end()返回指向字符串末尾(最后一个字符的下一个位置)的迭代器。常用于遍历字符串,例如配合循环或算法操作。
cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
for (auto it = str.begin(); it != str.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
输出
ebnf
H e l l o
1.6范围 for 循环支持
cpp
std::string str = "example";
for (char c : str) {
std::cout << c << " ";
}
输出:e x a m p l e
2.综合举例
| 函数名 | 功能 | 例子 |
|---|---|---|
| push_back | 字符串尾部插入一个字符 | s.push_back(' ') |
| append | 字符串尾部追加字符串 | s.append("world") |
| operator+= | 尾部追加字符 / 字符串(最常用) | s += "!!!"; s += '6'; |
| c_str | 返回 C 语言格式字符串 | s.c_str() |
| find | 从前往后查找,返回下标 | s.find('w') |
| rfind | 从后往前查找,返回下标 | s.rfind('!') |
| substr | 截取子串 | s.substr(6,5) |
| npos | 表示 "没找到" | if(pos == string::npos) |
cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 1. 基础字符串
string s = "hello";
// 2. push_back:尾插一个字符
s.push_back(' ');
cout << "push_back: " << s << endl;
// 3. append:追加字符串
s.append("world");
cout << "append: " << s << endl;
// 4. operator+= 重点:追加字符串/字符
s += "!!!";
s += '6';
cout << "operator+= : " << s << endl;
// 5. c_str 重点:转C语言字符串(const char*)
const char* cstr = s.c_str();
cout << "c_str: " << cstr << endl;
// 6. find 重点:从前往后找字符/字符串
size_t pos1 = s.find('w');
cout << "find 'w' : " << pos1 << endl;
// 7. rfind:从后往前找
size_t pos2 = s.rfind('!');
cout << "rfind '!' : " << pos2 << endl;
// 8. substr 重点:截取子串
string sub = s.substr(6, 5); // 从下标6开始,截5个字符
cout << "substr: " << sub << endl;
// npos 演示:没找到返回 npos
size_t pos3 = s.find('x');
if (pos3 == string::npos) {
cout << "find 'x' : not found" << endl;
}
return 0;
}
输出

3.利用string解决问题
给你一个字符串 s ,根据下述规则反转字符串:
所有非英文字母保留在原有位置。
所有英文字母(小写或大写)位置反转。
返回反转后的 s 。
示例 1:
输入:s = "ab-cd"
输出:"dc-ba"
示例 2:
输入:s = "a-bC-dEf-ghIj"
输出:"j-Ih-gfE-dCba"
示例 3:
输入:s = "Test1ng-Leet=code-Q!"
输出:"Qedo1ct-eeLg=ntse-T!"
cpp
#include <iostream>
#include <string>
using namespace std;
string reverseOnlyLetters(string s) {
int left = 0;
int right = s.size() - 1;
while (left < right) {
// 左边不是字母,右移
if (!isalpha(s[left])) {
left++;
}
// 右边不是字母,左移
else if (!isalpha(s[right])) {
right--;
}
// 都是字母,交换
else {
swap(s[left], s[right]);
left++;
right--;
}
}
return s;
}
谢谢