我是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函数需要int
类型的参数,因此我想将result[0].first
转换为int
。感谢帮助。
最佳答案
XY problem。您不需要int类型,需要该子字符串,并且可以通过就可以了的迭代器获取它。像这样:
splitList.emplace_back( iterStart, result[0].first );
关于c++ - 如何在C++中将std::string::const_iterator类型转换为int类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63027461/