c++ - 如何在C++中将std::string::const_iterator类型转换为int类型

标签 c++

我是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/

相关文章:

c++ - 为什么 HID 在 mac 中枚举 Apple Internal Keyboard/Trackpad 3 次?

c++ - (Qt) 从按钮的 QButtonGroup 创建信号?

c++ - 查看是否可以使用堆栈从左侧和右侧以相同的方式读取字符串

c++ - 如何从循环中获取4个字符而不是1个数据?

C++,程序在允许输入之前结束

C++ 未声明的标识符(但已声明?)

c++ - 有没有一种简单的方法可以将 C++ 对象添加到现有的 Objective-C Cocoa 应用程序

c++ - 无法在C++的标准输出流(cout)中输出按位运算的结果

c++ - 将数组值保存到 double 变量中

C++ - 通过复制行为返回