C++中的ios类的bad()方法用于检查流是否引发了任何错误。这意味着该函数将检查此流是否设置了其Badbit。
句法:
bool bad() const;
参数:此方法不接受任何参数。
返回值:如果流设置了badbit,则此方法返回true,否则返回false。
范例1:
// C++ code to demonstrate
// the working of bad() function
#include
using namespace std;
int main()
{
// Stream
stringstream ss;
// Using bad() function
bool isBad = ss.bad();
// print result
cout << "is stream bad: "
<< isBad << endl;
return 0;
}
输出:
is stream bad: 0
范例2:
// C++ code to demonstrate
// the working of bad() function
#include
using namespace std;
int main()
{
// Stream
stringstream ss;
ss.clear(ss.badbit);
// Using bad() function
bool isBad = ss.bad();
// print result
cout << "is stream bad: "
<< isBad << endl;
return 0;
}
输出:
is stream bad: 1
参考: hhttp://www.cplusplus.com/reference/ios/ios/bad/
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。