深入解析 C++ 字符串处理:提取和分割的多种方法

embedded/2025/2/2 13:18:19/

在 C++ 编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时。本文将详细探讨如何使用 C++ 标准库中的工具(如 std::istringstreamstd::string 的成员函数)来提取和分割字符串,并分析不同方法的适用场景和优缺点。我们将通过多个示例代码逐步讲解,帮助读者掌握字符串处理的技巧。


1. 字符串提取的基本方法

1.1 使用 std::istringstream>> 操作符

std::istringstream 是 C++ 标准库中的一个类,它将字符串作为输入流来处理。通过 >> 操作符,我们可以从流中提取以空格分隔的单词或数字。

示例代码
#include <iostream>
#include <sstream>
#include <string>int main() {std::string s = "id13 id1 id6 id0 id8 id6 id0";std::istringstream iss(s);std::string token;while (iss >> token) {std::cout << token << std::endl;}return 0;
}

输出

id13
id1
id6
id0
id8
id6
id0
分析
  • iss >> token 会按空格分隔字符串,逐个提取单词。

  • 这种方法适用于字符串中的单词是用空格分隔的简单场景。


1.2 提取 id 后面的数字

如果需要从类似 "id13 id1 id6" 的字符串中提取 id 后面的数字,可以使用 std::string::substr 方法。

示例代码
#include <iostream>
#include <sstream>
#include <string>
#include <vector>int main() {std::string s = "id13 id1 id6 id0 id8 id6 id0";std::istringstream iss(s);std::string token;std::vector<int> ids;while (iss >> token) {if (token.substr(0, 2) == "id") {int id = std::stoi(token.substr(2));ids.push_back(id);}}for (int id : ids) {std::cout << id << std::endl;}return 0;
}
输出
13
1
6
0
8
6
0
分析
  • token.substr(2)token 的第 2 个字符开始提取子串,跳过 "id"

  • 无论 id 后面的数字是一位数、两位数还是三位数,substr(2) 都能正确提取。

  • 这种方法简洁高效,适用于提取固定前缀后的数字。


2. 处理复杂分隔符

2.1 使用 std::getline 自定义分隔符

如果字符串的分隔符不是空格(例如逗号 , 或分号 ;),可以使用 std::getline 并指定分隔符。

示例代码
#include <iostream>
#include <sstream>
#include <string>
#include <vector>int main() {std::string s = "id13,id1,id6,id0,id8,id6,id0";std::istringstream iss(s);std::string token;std::vector<int> ids;while (std::getline(iss, token, ',')) {if (token.substr(0, 2) == "id") {int id = std::stoi(token.substr(2));ids.push_back(id);}}for (int id : ids) {std::cout << id << std::endl;}return 0;
}
输出
13
1
6
0
8
6
0
分析
  • std::getline(iss, token, ',') 会按逗号分隔字符串,逐个提取单词。

  • 这种方法适用于处理自定义分隔符的场景。


2.2 处理多行输入

如果输入是多行的,std::getline 也可以按行提取内容。

示例代码
#include <iostream>
#include <sstream>
#include <string>int main() {std::string s = "id13 id1 id6\nid0 id8 id6\nid0";std::istringstream iss(s);std::string line;while (std::getline(iss, line)) {std::istringstream lineStream(line);std::string token;while (lineStream >> token) {std::cout << token << std::endl;}}return 0;
}
输出
id13
id1
id6
id0
id8
id6
id0
分析
  • 外层 std::getline 按行提取内容。

  • 内层 lineStream >> token 按空格分隔每行的单词。

  • 这种方法适用于处理多行输入的场景。


3. 高级字符串处理技巧

3.1 使用正则表达式

C++11 引入了 <regex> 库,支持正则表达式匹配,可以更灵活地处理字符串。

示例代码
#include <iostream>
#include <regex>
#include <string>
#include <vector>int main() {std::string s = "id13 id1 id6 id0 id8 id6 id0";std::regex pattern(R"(id(\d+))");std::smatch matches;std::vector<int> ids;auto words_begin = std::sregex_iterator(s.begin(), s.end(), pattern);auto words_end = std::sregex_iterator();for (std::sregex_iterator i = words_begin; i != words_end; ++i) {std::smatch match = *i;int id = std::stoi(match.str(1));ids.push_back(id);}for (int id : ids) {std::cout << id << std::endl;}return 0;
}
输出
13
1
6
0
8
6
0
分析
  • 使用正则表达式 R"(id(\d+))" 匹配 id 后面的数字。

  • 这种方法功能强大,但语法较复杂,适合处理复杂的字符串匹配任务。


3.2 性能优化

对于大规模数据处理,性能可能成为瓶颈。可以通过以下方法优化:

  1. 避免频繁创建和销毁 std::istringstream 对象。

  2. 使用 std::string_view(C++17)减少字符串拷贝。

示例代码
#include <iostream>
#include <sstream>
#include <string>
#include <vector>int main() {std::string s = "id13 id1 id6 id0 id8 id6 id0";std::istringstream iss(s);std::string token;std::vector<int> ids;ids.reserve(10); // 预分配空间while (iss >> token) {if (token.substr(0, 2) == "id") {int id = std::stoi(token.substr(2));ids.push_back(id);}}for (int id : ids) {std::cout << id << std::endl;}return 0;
}
分析
  • 预分配 ids 的空间可以减少动态内存分配的开销。

  • 使用 std::string_view 可以避免不必要的字符串拷贝。


4. 总结

本文详细介绍了 C++ 中字符串提取和分割的多种方法,包括:

  1. 使用 std::istringstream>> 操作符按空格分隔字符串。

  2. 使用 std::getline 处理自定义分隔符和多行输入。

  3. 使用正则表达式处理复杂的字符串匹配任务。

  4. 通过性能优化技巧提高代码效率。

每种方法都有其适用的场景和优缺点,开发者可以根据具体需求选择合适的方法。掌握这些技巧后,你将能够高效地处理各种字符串任务,提升代码的可读性和性能。


通过本文的学习,希望读者能够深入理解 C++ 字符串处理的精髓,并在实际项目中灵活运用这些方法。


http://www.ppmy.cn/embedded/158909.html

相关文章

升级到Mac15.1后pod install报错

升级Mac后&#xff0c;Flutter项目里的ios项目运行 pod install报错&#xff0c; 遇到这种问题&#xff0c;不要着急去百度&#xff0c;大概看一下报错信息&#xff0c;每个人遇到的问题都不一样。 别人的解决方法并不一定适合你&#xff1b; 下面是报错信息&#xff1a; #…

读书笔记--分布式服务架构对比及优势

本篇是在上一篇的基础上&#xff0c;主要对共享服务平台建设所依赖的分布式服务架构进行学习&#xff0c;主要记录和思考如下&#xff0c;供大家学习参考。随着企业各业务数字化转型工作的推进&#xff0c;之前在传统的单一系统&#xff08;或单体应用&#xff09;模式中&#…

RabbitMQ 架构分析

文章目录 前言一、RabbitMQ架构分析1、Broker2、Vhost3、Producer4、Messages5、Connections6、Channel7、Exchange7、Queue8、Consumer 二、消息路由机制1、Direct Exchange2、Topic Exchange3、Fanout Exchange4、Headers Exchange5、notice5.1、备用交换机&#xff08;Alter…

Java动态代理:原理与实现

在Java编程中&#xff0c;代理模式是一种常见的设计模式&#xff0c;它允许我们通过一个代理对象来控制对另一个对象的访问。代理模式的主要目的是在不改变原始类代码的情况下&#xff0c;增强或修改其行为。Java中的代理分为静态代理和动态代理两种。本文将重点介绍动态代理&a…

计算机网络一点事(21)

第四章 网络层 功能&#xff1a;服务传输层&#xff0c;封装ip数据报&#xff08;主机到主机&#xff09; IP地址以32b表示&#xff0c;以8b为一组记十进制数 异构网络互连&#xff1a;网络结构&#xff0c;主机类型不同 路由器相互配合出IP数据报生成表&#xff0c;根据表…

数组排序算法

数组排序算法 用C语言实现的数组排序算法。 排序算法平均时间复杂度最坏时间复杂度最好时间复杂度空间复杂度是否稳定适用场景QuickO(n log n)O(n)O(n log n)O(log n)不稳定大规模数据&#xff0c;通用排序BubbleO(n)O(n)O(n)O(1)稳定小规模数据&#xff0c;教学用途InsertO(n)…

【Rust自学】15.3. Deref trait Pt.2:隐式解引用转化与可变性

喜欢的话别忘了点赞、收藏加关注哦&#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵&#xff01;(&#xff65;ω&#xff65;) 15.3.1. 函数和方法的隐式解引用转化(Deref Coercion) 隐式解引用转化(Deref Coercion)是为函数和方法提供的一种便捷特性。 它的原理是…

今日头条公域流量引流新径:开源 AI 智能名片 2 + 1 链动模式 S2B2C 商城小程序融合之道

摘要&#xff1a; 本文深度聚焦于今日头条平台庞大且持续增长的公域流量&#xff0c;全面探讨在平台严控流量外流背景下&#xff0c;如何行之有效地利用该平台进行私域流量引流&#xff0c;特别是将微信号作为私域承接载体的具体策略。同时&#xff0c;创新性地引入开源AI智能名…