我遇到了问题 - 我想将标准输入行的值存储到int但不确定如何转换为int:
string line;
int value;
getline(cin,line);
istringstream ss(line);
while (ss>>line) {
if (ss.eof()==false) {
// stores non ints in strings
}
else {
value=line; //ERROR
}
}
我试图使用标准文档材料转换它,但我无处可去。我做错了什么?
答案 0 :(得分:0)
您可以使用std::stoi
:
value=std::stoi(line);
答案 1 :(得分:0)
You could use your istringstream
to try loading a value into an int while checking for ss.fail()
.
ss >> temp; // where temp is an int
if(ss.fail())
{
// handle the error because the value wasn't an int
}
else
{
// process your int
}
This, naturally, could be modified a bit based on your needs.