![](/img/trans.png)
[英]why does a constant string become a bool when used for input to variant<bool, std::string>?
[英]Why doesn't the istringstream eof flag become true when successfully converting a boolean string value to a bool?
我正在学习如何使用istringstream
将存储为字符串的值转换为本机类型。 当存储为字符串的数字成功转换为int
或double
,istringstream eof()函数返回true。 当存储为字符串的布尔值成功转换为bool
,eof()返回false。
导致差异的原因是什么,当似乎没有其他字符可供处理时,eof()不会返回true?
转换为bool的代码:
string value = "true";
istringstream converter(value);
bool convertedValue;
if (!(converter >> std::boolalpha >> convertedValue)){
cout << "Conversion error." << endl;
} else {
cout << "Conversion success." << endl;
}
cout << "convertedValue=" << convertedValue << " value.length()=" << value.length() << " converter.tellg()=" << converter.tellg() << " converter.eof()=" << converter.eof() << endl;
输出显示eof标志为false:
Conversion success.
convertedValue=1 value.length()=4 converter.tellg()=4 converter.eof()=0
转换为双精度的代码:
string value = "1234.56";
istringstream converter(value);
double convertedValue;
if (!(converter >> std::boolalpha >> convertedValue)){
cout << "Conversion error." << endl;
} else {
cout << "Conversion success." << endl;
}
cout << "convertedValue=" << convertedValue << " value.length()=" << value.length() << " converter.tellg()=" << converter.tellg() << " converter.eof()=" << converter.eof() << endl;
输出显示eof标志为true:
Conversion success.
convertedValue=1234.56 value.length()=7 converter.tellg()=-1 converter.eof()=1
转换为int的代码:
string value = "1234";
istringstream converter(value);
int convertedValue;
if (!(converter >> std::boolalpha >> convertedValue)){
cout << "Conversion error." << endl;
} else {
cout << "Conversion success." << endl;
}
cout << "convertedValue=" << convertedValue << " value.length()=" << value.length() << " converter.tellg()=" << converter.tellg() << " converter.eof()=" << converter.eof() << endl;
输出显示eof标志为true:
Conversion success.
convertedValue=1234 value.length()=4 converter.tellg()=-1 converter.eof()=1
我正在使用g ++(Debian 4.8.3-3)4.8.3。
在第一次不成功的输入操作之后到达“文件结束”的状态,其中没有更多要输入的内容。
在从字符串读取的情况下,输入操作读取一个字符。
输入布尔值(“true”)不必尝试读取“e”之外的字符。 这与数字的输入操作形成对比,其中可能存在下一个数字。
要确定是否已读取全部:检查tellg结果是-1还是等于字符串长度。
根据C ++标准
获得范围[in,end]中的连续字符(参见23.2.3)并且仅在必要时与目标序列中的对应位置匹配以识别唯一匹配。
因此,例如,如果要编译以下代码
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
int main()
{
std::istringstream is( "trueabc" );
bool b = false;
is >> std::boolalpha >> b;
std::cout << std::boolalpha << b << std::endl;
std::cout << is.eof() << std::endl;
std::string s;
is >> s;
std::cout << s << std::endl;
return 0;
}
使用GCC(www.ideone.com)或MS VC ++ 2010,结果将是相同的
true
false
abc
这就足以从输入流中读取“true”以确定“ 唯一匹配 ”。
有趣的是,MS VC ++ 2010似乎包含一个bug。 如果要编译以下代码
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::istringstream is( "true" );
bool b = false;
is >> std::boolalpha >> b;
std::cout << std::boolalpha << b << std::endl;
std::cout << is.eof() << std::endl;
return 0;
}
然后输出将是
MS VC ++ 2010:
true
true
GCC:
true
false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.