C++ boolalpha confusion
C++ boolalpha confusion
嗨,有人能解释下面的c++代码结果吗?
input: true false 1
output: false, true, true,
#include <iostream>
using namespace std;
int main ()
{
bool c1, c2, c3;
cin >> c1 >> c2 >> c3;
cout << boolalpha << c1 << ", " << c2 << ", " << c3 << ", " << endl;//LINE I
return 0;
}
请参阅此处
- 通过调用str.setf(std::ios_base::boolalpha
务必先检查文档。
您操作的istream
对象(此处为cin
)需要0或1作为bool
的输入,而您的输入则为true
。这会导致操作失败,并在istream
上设置failbit
,因为您不检查输入是否失败,所以接下来对c2
和c3
的两个赋值确实发生了,而是被跳过。因此,由于输入操作失败,c1
为假。c2
和c3
没有被修改或初始化,因此可以具有任何值。
相关文章:
- Confusion: decltype vs std::function
- unique_ptr<char[]> confusion
- C++ Classes/Confusion
- C++ ifstream::read confusion
- Visual Studio 2013 linker confusion
- boost::range_iterator and boost::iterator_range confusion
- Pthreads, confusion with pthread_join(pthread_t, void**)
- C++ wParam and Lparam confusion
- C++ boolalpha confusion
- C++ Visual Studio Unicode confusion
- istringstream Confusion未返回正确值
- EDK SDK confusion
- C++ if/else confusion
- Confusion with #ifndef and #ifdef
- 布尔值转换为字符串,并将boolalpha与一个方法组合在一起,而不是在打印时单独键入它
- Qt C++ QTouchEvent & TouchPoint Confusion
- Confusion with c++11?