当我尝试使用从输入中提取有效数字时,istringstream
我得到以下错误行为istringstream
:
例如:
void extract(void)
{
double x;
string line, temp;
getline(cin, line);
istringstream is(line);
while(is >>temp)
{
if(istringstream(temp) >>x)
{std::cout<<"number read: "<<x<<endl;}
}
}
输入:
1 2 3rd 4th
输出:
number read: 1
number read: 2
number read: 3
number read: 4
不当行为是 istringstream 将字符串转换3rd
为数字 3。
为什么istringstream
要这样做,如何避免这种情况?