我是 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;
}
Run Code Online (Sandbox Code Playgroud)
的类型result[0].first是std::string::const_iterator,并且str.substr函数需要类型的参数int,所以我想转换result[0].first为int类型的
感谢帮助。
XY问题。你不想要一个 int 类型,你想要那个子字符串,你可以通过你拥有的迭代器来获取它。像这样:
splitList.emplace_back( iterStart, result[0].first );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
146 次 |
| 最近记录: |