簡體   English   中英

如何理解std :: istringstream的結果?

[英]how to understand the result of std::istringstream?

int x;
float f;
std::string s = "3.7";

std::istringstream is(s);
is >> x >> f;

為什么“ x”和“ f”的結果分別為3和0.7?
當我使用

is >> f;

f的結果是3.7。

istreamstream從流中提取盡可能適合目標數據類型的字符(請參閱算術類型)。 另一個停止條件是空格。

因此,如果您將代碼更改為以下內容:

int x;
char c;
int f;
std::string s = "3.7";

std::istringstream is(s);
is >> x;
is >> c;
is >> f;

std::cout << x << std::endl;
std::cout << c << std::endl;
std::cout << f << std::endl;

這將導致以下輸出:

3
.
7

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM