从 std::string C++中提取任意数据值
Extract arbitrary data values from a std::string C++
我有这样的字符串
10z45
9999i4a
基本上int-char-int-optionalchar
我想做这个函数原型
void process(std::string input, int &first, char &c, int &last, bool &optional)
唯一的问题是我不确定迭代字符串以提取这些值的最佳方法。 宁愿不使用正则表达式库,似乎可以简单地完成?
使用字符串流:
#include <sstream>
...
std::istringstream iss(input);
iss >> first >> c >> last >> optional;
如果没有最终字符,则不会触及optional
的值,因此我建议事先将其设置为 0。
使用
std::istringstream
,读取 int、char、int,然后尝试下一个 char:
std::istringstream is(input);
is >> first >> c >> last;
char c2;
optional = (is >> c2);
我不确定这是你想要的 100% - 但我会这样做。
相关文章:
- 从函数返回任意简单类型的数据
- 在 std::vector<无符号字符中存储任意数据的方法>
- 如何在C++中实现节点数任意的通用树数据结构?
- 将任意数据传递给不接受"void* userarg"的C++回调
- 使用 std::function 作为具有任意数量参数的数据成员
- 创建一个模板函数来读取 hdf5 中的任意数据类型
- C++标准是否允许复制任意多态数据结构?
- QT:使用QVariant任意复杂的数据结构
- 将任意的JavaScript数据对象传递到Node.js C 插件
- 在任意位置进行快速查找和删除的Deque样数据结构
- 从具有任意空间量的文件中获取数据集
- 从 std::string C++中提取任意数据值
- 用C++对作为数据给出的任意函数进行有效求值
- 递归类型真的是构建不连续的任意大小数据结构的唯一方法吗
- C++类中的任意数据类型
- 将任意数据传递给没有空指针的函数
- 如何使用OpenSSL编码/解码任意数据
- 如何按任意列对数据进行动态排序
- 期望eof,而不是eof附近的任意数据
- <algorithm> 对任意数据类型使用库