2012-04-04 71 views
19

我有一个程序需要输入一些选项来指定表单中的概率(p1,p2,p3,...)。因此,在命令行中使用会从字面上是:在两个迭代器之间获取`std :: string`的子字符串

./myprog -dist (0.20, 0.40, 0.40) 

我想分析这样的列表中C++和我目前正在与迭代器在std :: string类型和升压提供的分割功能做到这一点。

// 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(", ")); 

注意上面的<what to put here?>部分。因为我需要忽略开始和结束括号,我要像做

tmp1.substr(++tmp1.begin(), --tmp1.end()) 

,但它并不像substr作品就这样了,我找不到工作要做到这一点的文件中的函数。

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

tmp1.substr(++tmp1.begin(), (--tmp1.end()) - (++tmp1.begin())) 

,但如果这是允许的,我不知道,或者如果它是一个合理的方式做到这一点。如果这不是一种有效的方法,那么更好的方法是什么? ...提前谢谢了。

回答

39

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

std::string(tmp1.begin() + 1, tmp1.end() - 1) 
相关问题