![](/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.