在两个迭代器之间获取`std :: string`的子字符串

ely*_*ely 26 c++ string parsing

我有一个程序,预计会将一些选项作为输入,以指定形式(p1,p2,p3,...)中的概率.所以命令行的用法实际上是:

./myprog -dist (0.20, 0.40, 0.40)
Run Code Online (Sandbox Code Playgroud)

我想在C++中解析这样的列表,我目前正在尝试使用std :: string类型的迭代器和Boost提供的split函数.

// Assume stuff up here is OK
std::vector<string> dist_strs; // Will hold the stuff that is split by boost.
std::string tmp1(argv[k+1]);   // Assign the parentheses enclosed list into this std::string.

// Do some error checking here to make sure tmp1 is valid.

boost::split(dist_strs,  <what to put here?>   , boost::is_any_of(", "));
Run Code Online (Sandbox Code Playgroud)

注意上面的<what to put here?>部分.因为我需要忽略开头和结尾的括号,我想做类似的事情

tmp1.substr( ++tmp1.begin(), --tmp1.end() )
Run Code Online (Sandbox Code Playgroud)

但它看起来不像substr这样,我无法在文档中找到有效的功能.

我有一个想法是做迭代器算法,如果这是允许的,并用于substr调用

tmp1.substr( ++tmp1.begin(), (--tmp1.end()) - (++tmp1.begin()) )
Run Code Online (Sandbox Code Playgroud)

但我不确定这是否被允许,或者它是否是一种合理的方式.如果这不是一个有效的方法,那么更好的方法是什么?...提前谢谢了.

Pub*_*bby 54

std::string的构造函数应该提供您需要的功能.

std::string(tmp1.begin() + 1, tmp1.end() - 1)
Run Code Online (Sandbox Code Playgroud)