跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像AI 生图工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
C++

C++ STL string 类核心用法与接口详解

C++ STL string 类涵盖构造、容量管理、访问遍历及修改操作。通过示例展示了空串、C 风格字符串及拷贝构造等初始化方式;解析了 size、capacity、empty 等容量查询函数,区分了 reserve 预分配与 resize 调整长度的差异。内容还包含下标与迭代器访问、push_back 尾插、append 追加、find 查找及 substr 截取等常用接口,最后补充了运算符重载与非成员函数的输入输出处理,为实际开发提供完整参考。

清酒独酌发布于 2026/3/22更新于 2026/7/2030 浏览
C++ STL string 类核心用法与接口详解

前言

STL 中的 string 类是处理文本数据最常用的容器之一。在掌握迭代器、auto 关键字和范围 for 循环的基础上,深入理解 string 的常用接口能显著提升开发效率。本文将重点梳理 string 类的构造、容量管理、访问遍历及修改操作。

一、string 类对象的常见构造

1.1 空字符串构造函数

无参构造函数 string() 会创建一个长度为 0 的空字符串对象。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s1; // 调用默认构造函数,构造一个空字符串
    cout << "s1: " << s1 << endl; 
    cout << "s1.size(): " << s1.size() << endl; // 输出 0
    return 0;
}

1.2 C 字符串构造函数

可以通过 C 风格字符串(const char*)直接初始化 string 对象。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s2("Hello world"); // 通过 C 类型字符进行构造
    cout << "s2: " << s2 << endl;
    return 0;
}

1.3 n 个字符构造函数

指定数量 n 和字符 c,构造包含 n 个 c 的字符串。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s3(5, 'x'); // 通过 n 个字符进行构造
    cout << "s3: " << s3 << endl;
    return 0;
}

1.4 拷贝构造函数

支持通过另一个 string 对象进行拷贝初始化。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string original = "Hello C++";
    string str(original); // 调用拷贝构造函数
    cout << "str: " << str << endl; // 输出 "Hello C++"
    return 0;
}

二、string 类的容量操作

2.1 size

返回字符串的实际长度(字节数)。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello World";
    cout << "s.size(): " << s.size() << endl; // 输出 11
    return 0;
}

2.2 capacity

返回当前为字符串分配的存储空间大小,通常大于或等于 size。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s1("Hello World");
    cout << "s1.capacity(): " << s1.capacity() << endl;
    return 0;
}

2.3 empty

检查字符串是否为空,返回 bool 值。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s1 = "hello world";
    string s2;
    cout << "s1.empty(): " << s1.empty() << endl; // false
    cout << "s2.empty(): " << s2.empty() << endl; // true
    return 0;
}

2.4 clear

清空字符串内容,使其变为空串,但可能保留原有容量。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello World";
    s.clear();
    cout << "After clear, s: " << s << endl; // 输出空
    cout << "s.size(): " << s.size() << endl; // 输出 0
    cout << "s.capacity(): " << s.capacity() << endl; // 容量可能不变
    return 0;
}

2.5 reserve

请求将字符串容量调整为至少 n 个字符。这不会改变字符串长度,仅影响底层内存分配,常用于避免频繁扩容。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1;
    string str2;
    
    cout << "=== 不使用 reserve ===" << endl;
    cout << "初始 str1 容量:" << str1.capacity() << ", 长度:" << str1.length() << endl;
    str1 = "This is a demonstration of the string reserve function in C++.";
    cout << "赋值后 str1 容量:" << str1.capacity() << ", 长度:" << str1.length() << endl;
    
    cout << "\n=== 使用 reserve ===" << endl;
    cout << "初始 str2 容量:" << str2.capacity() << ", 长度:" << str2.length() << endl;
    str2.reserve(100); // 预先分配至少 100 个字符的内存
    cout << "reserve(100) 后,str2 容量:" << str2.capacity() << ", 长度:" << str2.length() << endl;
    str2 = "This is a demonstration of the string reserve function in C++.";
    cout << "赋值后 str2 容量:" << str2.capacity() << ", 长度:" << str2.length() << endl;
    return 0;
}

2.6 resize

调整字符串的实际长度。若新长度小于原长度则截断;若大于则填充(可指定填充字符)。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "Hello C";
    s.resize(9, '+'); // 扩展至 9 个字符,不足部分用 '+' 填充
    cout << "s: " << s << endl; // 输出 "Hello++++"
    cout << "s.size(): " << s.size() << endl;
    
    s.resize(5); // 截断至 5 字符
    cout << "s: " << s << endl; // 输出 "Hello"
    cout << "s.size(): " << s.size() << endl;
    return 0;
}

三、string 类的访问及遍历操作

3.1 operator[]

支持下标访问,返回字符引用,可读写。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s2("hello world");
    cout << "下标访问元素:";
    for (int i = 0; i < s2.size(); i++) {
        cout << s2[i] << " ";
    }
    
    s2[0] = 'H';
    cout << "\n更改后下标位置 2 处的字符串:";
    for (int i = 0; i < s2.size(); i++) {
        cout << s2[i] << " ";
    }
    return 0;
}

3.2 迭代器获取:begin 和 end

通过迭代器可以遍历并修改字符串内容。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s2("hello world");
    cout << "迭代器访问元素:";
    string::iterator it = s2.begin();
    while (it != s2.end()) {
        cout << *it << " ";
        ++it;
    }
    
    cout << "\n利用迭代器更改元素:";
    it = s2.begin();
    while (it != s2.end()) {
        *it += 2;
        cout << *it << " ";
        ++it;
    }
    return 0;
}

3.3 范围 for 循环

现代 C++ 推荐写法,简洁且高效。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s2("hello world");
    cout << "范围 for 循环访问元素:";
    for (auto& ch : s2) {
        cout << ch << " ";
    }
    
    cout << "\n范围 for 循环更改元素:";
    for (auto& ch : s2) {
        ch += 2;
        cout << ch << " ";
    }
    return 0;
}

四、string 类的修改操作

4.1 push_back

向字符串末尾追加单个字符。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s("hello world");
    cout << "尾插前的 s:" << s << endl;
    s.push_back(' ');
    s.push_back('s');
    cout << "push_back 尾插字符后的 s:" << s << endl;
    return 0;
}

4.2 operator+= 和 append

operator+= 更常用,支持追加字符、C 字符串或 string 对象;append 功能类似但略显繁琐。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s("hello world");
    cout << "尾插前的 s:" << s << endl;
    
    s += 'x';
    cout << "尾插单个字符的 s:" << s << endl;
    
    s += " Welcome";
    cout << "尾插 C 类型字符串后的 s:" << s << endl;
    
    string s2(" Hello C++");
    s += s2;
    cout << "尾插字符串 s2 后的 s:" << s << endl;
    return 0;
}

4.3 c_str

转换为 C 风格字符串指针,常用于与旧 API 交互。

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
    string s = "C++";
    const char* p = s.c_str();
    cout << "C-string: " << p << endl;
    cout << "Length via strlen: " << strlen(p) << endl;
    return 0;
}

4.4 find

查找子串或字符首次出现的位置,未找到返回 string::npos。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s("text.cpp.zip");
    size_t pos1 = s.find('.');
    cout << "字符串 s 中 . 的位置为:" << pos1 << endl;
    
    size_t pos2 = s.find("cpp");
    cout << "字符串 s 中 cpp 的位置为:" << pos2 << endl;
    
    size_t pos3 = s.find("C++");
    if (pos3 == string::npos) {
        cout << "Not find!" << endl;
    }
    return 0;
}

4.5 substr

截取子字符串,返回新的 string 对象。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "We think in generalities, but we live in details.";
    string str2 = str.substr(3, 5); // "think"
    cout << "截取得到下标从 3 开始之后的五个字符:" << str2 << endl;
    
    size_t pos = str.find("live");
    string str3 = str.substr(pos); // 从 "live" 到结尾
    cout << "截取 pos 位置之后的字符串:" << str3 << '\n';
    return 0;
}

4.6 insert

在指定位置插入内容。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s("hello world");
    cout << "尾插前的 s:" << s << endl;
    
    s.insert(0, " Byte ");
    cout << "insert 在下标 0 处插入字符串后的 s:" << s << endl;
    
    string s2(" Welcome to C++");
    size_t pos = s.size();
    s.insert(pos, s2);
    cout << "insert 在下标 pos 处插入字符串后的 s:" << s << endl;
    
    s.insert(0, 5, '6');
    cout << "insert 在下标 0 处插入 5 个字符后的 s:" << s << endl;
    return 0;
}

4.7 erase

删除指定范围的字符。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str("This is an example sentence.");
    cout << str << '\n';
    
    // 从索引 10 开始,删除 8 个字符
    str.erase(10, 8);
    cout << str << '\n';
    return 0;
}

五、string 类的非成员函数

5.1 operator+

用于拼接两个字符串,返回新对象。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string firstlevel("com");
    string secondlevel("cplusplus");
    string scheme("http://");
    string hostname;
    string url;
    
    hostname = "www." + secondlevel + '.' + firstlevel;
    url = scheme + hostname;
    cout << url << '\n';
    return 0;
}

5.2 operator>> 和 operator<<

标准输入输出流的重载,>> 遇空格停止,<< 直接输出。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cout << "Enter a word: ";
    cin >> s;
    cout << "You entered: " << s << endl;
    
    string name = "Alice";
    cout << "Name: " << name << endl;
    return 0;
}

5.3 getline

读取整行输入,包括空格,直到遇到换行符或指定分隔符。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string line;
    cout << "Enter a line: ";
    getline(cin, line);
    cout << "Line: " << line << endl;
    
    getline(cin, line, ';');
    cout << "Until ';': " << line << endl;
    return 0;
}

5.4 字符串大小比较

支持字典序比较(==, !=, <, >, <=, >=)。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string foo = "alpha";
    string bar = "beta";
    
    if (foo == bar) cout << "foo and bar are equal\n";
    if (foo != bar) cout << "foo and bar are not equal\n";
    if (foo < bar) cout << "foo is less than bar\n";
    if (foo > bar) cout << "foo is greater than bar\n";
    if (foo <= bar) cout << "foo is less than or equal to bar\n";
    if (foo >= bar) cout << "foo is greater than or equal to bar\n";
    return 0;
}

目录

  1. 前言
  2. 一、string 类对象的常见构造
  3. 1.1 空字符串构造函数
  4. 1.2 C 字符串构造函数
  5. 1.3 n 个字符构造函数
  6. 1.4 拷贝构造函数
  7. 二、string 类的容量操作
  8. 2.1 size
  9. 2.2 capacity
  10. 2.3 empty
  11. 2.4 clear
  12. 2.5 reserve
  13. 2.6 resize
  14. 三、string 类的访问及遍历操作
  15. 3.1 operator[]
  16. 3.2 迭代器获取:begin 和 end
  17. 3.3 范围 for 循环
  18. 四、string 类的修改操作
  19. 4.1 push_back
  20. 4.2 operator+= 和 append
  21. 4.3 c_str
  22. 4.4 find
  23. 4.5 substr
  24. 4.6 insert
  25. 4.7 erase
  26. 五、string 类的非成员函数
  27. 5.1 operator+
  28. 5.2 operator>> 和 operator<<
  29. 5.3 getline
  30. 5.4 字符串大小比较
  • 免费图片AI生成工具免费生成了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 免费图片视频在线生成30秒,将你的创意变成现实开始设计
  • X/Twitter免费视频下载器免登陆无限额度免费视频解析下载了解详情
  • 100+免费在线小游戏爽一把
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • 使用基础模型自动化寻找人工生命 - MIT&OpenAI&Sakana AI
  • DeepSeek 复现狂潮:低成本强化学习实现开源推理
  • Python venv 虚拟环境工具使用指南及 uv 升级教程
  • Python 数据库连接池深度解析与调优实战
  • Whisper 语音识别技术本地部署与应用指南
  • Go 与 C++ 对比:性能、并发与生态差异分析
  • Ubuntu 虚拟机部署 OpenClaw 个人 AI 助手
  • 大模型的核心功能与技术架构解析
  • Java 随机数生成实战:解析范围字符串与动态区间控制
  • 使用 PyTorch 构建 GPT 模型
  • CSS 背景样式详解:颜色、图片与定位
  • Java Web 自研框架 18 年架构决策复盘与核心思路
  • 企业级Python反爬虫技术:JS逆向、APP抓包与验证码破解实战
  • 国产 AI 智能体工具横向测评:腾讯、字节、阿里等主流方案
  • AI Agent 生产级框架实战:架构设计与核心代码实现
  • 基于 Java 和 Leaflet 的湖南省道路长度 WebGIS 系统构建
  • PyCharm 中 Copilot 无法加载 Claude 模型解决方案
  • Byzer-lang 低代码 AI 数据平台部署指南
  • 网络安全行业自学与转行指南:入行规划与学习路径
  • Java Stream API 排序流特性与用法

相关免费在线工具

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

  • HTML转Markdown

    将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online

  • JSON 压缩

    通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online

  • JSON美化和格式化

    将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online