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

时间:2014-06-14 12:13:31

标签: c++ c++11

我正在学习如何使用istringstream将存储为字符串的值转换为本机类型。当存储为字符串的数字成功转换为intdouble时,istringstream eof()函数返回true。当存储为字符串的布尔值成功转换为bool时,eof()返回false。

导致差异的原因是什么,当似乎没有其他字符可供处理时,eof()为什么不返回true?

转换为布尔的代码:

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。

2 个答案:

答案 0 :(得分:3)

在第一次输入失败的操作后,达到“文件结束”的状态,此时无需输入任何内容。

在从字符串读取的情况下,输入操作会读取一个字符。

输入布尔值(“true”)不必尝试读取“e”之外的字符。这与数字的输入操作形成对比,数字的输入操作可能存在下一个数字。

确定是否已读取全部:检查tellg结果是-1还是等于字符串长度。

答案 1 :(得分: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

这就足以阅读&#34; true&#34;来自输入流,用于确定&#34; 唯一匹配&#34;。

有趣的是,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