Stringstream提取整数 [英] Stringstream extract integer
本文介绍了Stringstream提取整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我不能将整数值提取到 Num
变量中?
Why do I fail to extract an integer value into the Num
variable?
#include <sstream>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
string Digits("1 2 3");
stringstream ss(Digits);
string Temp;
vector<string>Tokens;
while(ss >> Temp)
Tokens.push_back(Temp);
ss.str(Tokens[0]);
int Num = 0;
ss >> Num;
cout << Num; //output: 0
}
推荐答案
当流提取3个digist"1 2 3"的最后一个时,将设置eof状态.str()成员未清除此消息,您需要自己执行此操作.将您的代码更改为:
When the stream extracts the last of the 3 digist "1 2 3" the eof state will be set. This is not cleared by the str() member,you need to do it yourself. Change your code to:
ss.clear();
ss.str(Tokens[0]);
这篇关于Stringstream提取整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文