C++ 如何判断对变量的流提取是否失败?

C++ 如何判断对变量的流提取是否失败?,c++,C++,我正在将数据从字符串流提取到string和double: std::string word; double num; std::istringstream stream("hello x"); stream >> word >> num; std::cout << word; std::cout << num; std::字符串字; 双数; std::istringstream流(“hello x”); 流>>word>>num; std::

我正在将数据从字符串流提取到
string
double

std::string word;
double num;
std::istringstream stream("hello x");

stream >> word >> num;

std::cout << word;
std::cout << num;
std::字符串字;
双数;
std::istringstream流(“hello x”);
流>>word>>num;
std::cout提取操作符(>>)将返回true或false,以确定提取是否成功

if (stream >> num)
  cout << "success\n";
else
  cout << "failed\n";
提取运算符(>>)将在提取是否成功时返回true或false

if (stream >> num)
  cout << "success\n";
else
  cout << "failed\n";

extraction运算符返回对流的引用(不是布尔值)。流在失败时会进入失败状态,并且流会隐式转换为布尔值,以检查相关的失败位。这非常便于了解。谢谢你的快速回答,@nkryptics!感谢您的澄清@MattMcNabb。提取运算符返回对流的引用(不是布尔值)。流在失败时会进入失败状态,并且流会隐式转换为布尔值,以检查相关的失败位。这非常便于了解。谢谢你的快速回答,@nkryptics!我感谢你在MattMcNabb的澄清。