如何判断对变量的流提取是否失败
How can you tell if a stream extraction to a variable failed?
我正在将字符串流中的数据提取到string
和double
:中
std::string word;
double num;
std::istringstream stream("hello x");
stream >> word >> num;
std::cout << word;
std::cout << num;
是否可以判断第二个令牌"x"
是否成功解析为int
?
在这种情况下,它显然不会,并且x的值是0
。
提取运算符(>>)将返回true或false以判断提取是否成功。
if (stream >> num)
cout << "successn";
else
cout << "failedn";
此外,您可能会在istringstream
对象上看到故障位设置
if (stream.fail())
cout << "failbit is setn";
相关文章:
- 如果我从流中提取某些内容,该流是否不再包含我提取的内容?
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- 如果 std::numeric_limits<float>::is_iec559 为真,这是否意味着我可以以明确定义的方式提取指数和尾数?
- 有谁知道ziplib是否能够在不实际提取所有文件的情况下验证zip库
- 是否可以使用一行代码从 std::cin 中提取格式化输入
- 是否可以为给定的静态库人为地诱导对象文件提取
- 是否保证标准提取运算符>>在失败时不会更改参数?
- 是否可以从 file.lib 中提取对象文件
- 是否可以在正则表达式中命名子图案,然后通过C 中的子图案名称提取匹配
- 是否可以从 clang 的解析树中提取预处理器信息?
- 是否可以提取 lambda 的捕获列表
- 如何提取并检查数组元素中的第一个字符是否为字母
- 提取运算符是否删除分隔符
- 是否可以从QListWidgetItem*中提取行
- 如何判断对变量的流提取是否失败
- 是否有用于从HTML页面中提取数据的库
- 是否可以使用函数ExtTextOut从屏幕中提取文本
- 是否可以在 c++ 中提取容器模板类
- c++文件提取检查是否为int
- 对某些标准流类插入和提取浮点数是否保留其值