简体   繁体   English

为什么在成功将布尔字符串值转换为bool时,istringstream eof标志是否成为真?

[英]Why doesn't the istringstream eof flag become true when successfully converting a boolean string value to a bool?

I am learning how to convert values stored as a string to native types using istringstream . 我正在学习如何使用istringstream将存储为字符串的值转换为本机类型。 When a number stored as a string is successfully converted to an int or double , the istringstream eof() function returns true. 当存储为字符串的数字成功转换为intdouble ,istringstream eof()函数返回true。 When a boolean stored as a string is successfully converted to a bool , eof() returns false. 当存储为字符串的布尔值成功转换为bool ,eof()返回false。

What causes the difference and why does eof() not return true when there seems to be no other characters remaining to process? 导致差异的原因是什么,当似乎没有其他字符可供处理时,eof()不会返回true?

Code for converting to a bool: 转换为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;

The output shows the eof flag is false: 输出显示eof标志为false:

Conversion success.
convertedValue=1  value.length()=4  converter.tellg()=4  converter.eof()=0

Code for converting to a double: 转换为双精度的代码:

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;

The output shows the eof flag is true: 输出显示eof标志为true:

Conversion success.
convertedValue=1234.56  value.length()=7  converter.tellg()=-1  converter.eof()=1

Code for converting to an int: 转换为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;

The output shows the eof flag is true: 输出显示eof标志为true:

Conversion success.
convertedValue=1234  value.length()=4  converter.tellg()=-1  converter.eof()=1

I am using g++ (Debian 4.8.3-3) 4.8.3. 我正在使用g ++(Debian 4.8.3-3)4.8.3。

The state of "End of File" is reached after the first unsuccessful input operation, where there is nothing more to input. 在第一次不成功的输入操作之后到达“文件结束”的状态,其中没有更多要输入的内容。

In the case of reading from a string, an input operation reads one character. 在从字符串读取的情况下,输入操作读取一个字符。

Inputting a boolean ("true") does not have to try and read a character beyond the 'e'. 输入布尔值(“true”)不必尝试读取“e”之外的字符。 This is in contrast to input operations for numbers, where there may be a next digit. 这与数字的输入操作形成对比,其中可能存在下一个数字。

To determine, whether all has been read: check whether the tellg result is -1 or equal to the string length. 要确定是否已读取全部:检查tellg结果是-1还是等于字符串长度。

According to the C++ Standard 根据C ++标准

Successive characters in the range [in,end) (see 23.2.3) are obtained and matched against corresponding positions in the target sequences only as necessary to identify a unique match. 获得范围[in,end]中的连续字符(参见23.2.3)并且仅在必要时与目标序列中的对应位置匹配以识别唯一匹配。

So for example if to compile the following code 因此,例如,如果要编译以下代码

#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;
}

using GCC (at www.ideone.com) or MS VC++ 2010 then the result will be the same 使用GCC(www.ideone.com)或MS VC ++ 2010,结果将是相同的

true
false
abc

That is it was enough to read "true" from the input stream that to determine the " unique match ". 这就足以从输入流中读取“true”以确定“ 唯一匹配 ”。

It is interesting to note that it seems that MS VC++ 2010 contains a bug. 有趣的是,MS VC ++ 2010似乎包含一个bug。 If to compile the following code 如果要编译以下代码

#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;
}

then the output will be 然后输出将是

MS VC++ 2010: MS VC ++ 2010:

true
true

GCC: GCC:

true
false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么常量字符串用于变体输入时会变成布尔值 <bool, std::string> ? - why does a constant string become a bool when used for input to variant<bool, std::string>? 转换字符串令牌流时如何避免重复的istringstream构建 - How to avoid repeated istringstream construction when converting stream of string tokens 将对象转换为bool时,显式说明符似乎不起作用 - explicit specifier doesn't seem to work when converting an object to bool 使用 string_view 构建 istringstream 无法编译 - Constructing istringstream with string_view doesn't compile 输出istringstream到字符串时使用setw,为什么长度不对? - Using setw when output istringstream to a string, why the length is not right? 提取运算符在 istringstream 上达到 EOF,使用 int/char/string 改变行为 - Extraction Operator reaching EOF on istringstream, behavioral change with int/char/string 当bool = true时,为什么补码运算符不起作用? - Why is the complement operator not working when bool = true? 为什么要用!! 将 int 转换为 bool 时? - Why use !! when converting int to bool? 不使用运算符时,为什么警告“强制将值强制设置为“ true”或“ false”)消失了? - Why warning “forcing value to bool 'true' or 'false'” disappears when using not operator? 为什么这个数组不为零? - Why doesn't this array become zero?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM