我试图通过boost::lexical_cast
子串(由一对迭代的表示)转换为整数:意外结果与升压:: iterator_range的
#include <iostream>
#include <boost/lexical_cast.hpp>
int main()
{
// assume [first, last) as substring
const std::string s("80");
auto first = s.begin(), last = s.end();
std::cout << boost::lexical_cast<int>(boost::make_iterator_range(first, last)) << std::endl;
return 0;
}
输出:(wandbox)
1
我得到了预期结果(80)的解决方法:boost::make_iterator_range(&*first, last - first)
。
问题:为什么上面的代码不能按预期方式工作?而且,1
从哪里来?
lexical_cast
不支持iterator_range<std::string::(const_)iterator>
滥用lexical_cast
lexical_cast
或iterator_range
- 错误或
iterator_range
- 一些其他原因
优秀的分析。 – sehe
很好的回答,谢谢! – kakkoko