我想知道什么是While(cin)测试
I was wondering what does While(cin) test?
我正在看Bjarne Stroustrup写的《Software - Principles and Practice using c++》,下面的代码是:
Token get_token();
vector<Token>tok;
int main()
{
while(cin)
{
Token t = get_token();
tok.push_back(t);
}
}
while检查什么?
while
将其条件表达式结果强制转换为bool
。据此,iostreams调用std::ios::operator bool:
返回是否设置了错误标志(
failbit
或badbit
)。注意,这个函数返回的不是成员
good
,而是成员fail
的反面。
模型示例显示了设置哪些位以及何时设置:
#include <iostream>
#include <iomanip>
void foo(std::istream& in, std::ostream& out) {
std::string str;
out << "goodbit | eofbit | failbit | badbit | string" << std::endl;
while(true) {
in >> str;
auto s = in.rdstate();
out
<< std::setw(7) << bool(s & std::ios::goodbit) << " | "
<< std::setw(6) << bool(s & std::ios::eofbit) << " | "
<< std::setw(7) << bool(s & std::ios::failbit) << " | "
<< std::setw(6) << bool(s & std::ios::badbit) << " | ";
if(in) {
out << str << std::endl;
}
else {
out << std::endl;
break;
}
}
}
int main(void) {
foo(std::cin, std::cout);
return 0;
}
$ echo "a ab" | ./untitled
(输入是管道)打印
goodbit | eofbit | failbit | badbit | string
0 | 0 | 0 | 0 | a
0 | 0 | 0 | 0 | ab
0 | 1 | 1 | 0 |
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- C++中的cin.ignore()函数不适用于整个流
- 尽管测试成功,CppUnit测试核心仍被丢弃.为什么
- 数据成员SFINAE的C++17测试:gcc vs clang
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- Problems with std::cin.fail()
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么二进制搜索在我的测试中不起作用
- 从父数组测试用例构造二叉树失败
- 试图对缓存进行跨步测试,但程序并没有结束
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为测试目标创建具有不同源文件夹的文件
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 通过 cin/cout 的协议单元测试 - 如何阻止流以等待内容?
- 如何使用cin测试空白行
- c++ cin.ignore忽略空格、数字和字符来测试回文
- 我想知道什么是While(cin)测试
- 如何使用cin进行单元测试