c++ - 如何在 C++ 中将 std::string::const_iterator 类型转换为 int 类型
问题描述
我是 C++ 的新手
我想用c++实现字符串拆分功能,所以写了如下代码:
#include <vector>
#include <regex>
#include <sstream>
#include <string>
std::string str = "1:2-3:4";
std::smatch result;
std::regex pattern("[:-]");
std::string::const_iterator iterStart = str.begin();
std::string::const_iterator iterEnd = str.end();
std::vector<std::string> splitList = {};
while (regex_search(iterStart, iterEnd, result, pattern))
{
// If std::string::const_iterator provides the int attribute
// splitList.push_back(str.substr(iterStart.int, result[0].first.int));
iterStart = result[0].second;
}
的类型result[0].first
是std::string::const_iterator
,而str.substr函数需要type的参数int
,所以我想转换result[0].first
成int
Types of
感谢帮助。
解决方案
XY 问题。你不想要一个 int 类型,你想要那个子字符串,你可以通过你拥有的迭代器来获得它。像这样:
splitList.emplace_back( iterStart, result[0].first );
推荐阅读
- go - 在哪里存储测试(项目结构 - 最佳实践)?
- php - 如何使用 +1 更新数据库
- html - 如何在闪亮的另一个 CSS 元素下居中
- flutter - 如何从 Flutter Web 的 HtmlElementView 执行 iframe 功能
- python - 如何在海龟重复中更改颜色
- angular - Angular - ErrorHandler 与 HttpInterceptor
- javascript - JS 如何创建图像并立即将其置于开始和结束样式之间的过渡?
- c++ - 当另一个函数已经在运行时运行一个函数
- javascript - 如何验证文本输入作为电子邮件地址 - Jquery
- c# - 空引用异常 Unity 2d