这里有个小问题:
int IntegerTransformer::transformFrom(std::string string){
stream->clear();
std::cout<<string<<std::endl;;
(*stream)<<string;
int i;
(*stream)>>i;
std::cout<<i<<std::endl;
return i;
}
我用字符串“67”调用这个函数(其他值也不起作用)我得到这个输出:
67
6767
最佳答案
您是否注意到函数本身有两个 std::cout
?
除此之外还要添加:
stream->str(""); //This ensures that the stream is empty before you use it.
(*stream)<<string;
顺便问一下,你为什么不使用 boost::lexical_cast
?
int IntegerTransformer::transformFrom(std::string s){
return boost::lexical_cast<int>(s);
}
关于c++ - 使用 Stringstream 将字符串转换为 Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939260/