您关于如何完成此任务scanf
的问题printf
似乎是一个XY 问题。@GSliepen 提供的答案向您展示了如何在 C++ 中正确执行此操作。
但是,如果您真的对如何使用scanf
and来完成此操作感兴趣printf
(我不推荐),我会直接回答您的问题:
您不能scanf
直接与位集一起使用,因为scanf
至少需要一个字节的地址,而不是单个位。甚至不存在单个位的地址(至少在大多数平台上)。但是,您可以先使用scanf
写入临时字节 ( unsigned char
) 或 an int
,然后将其转换为 bitset 的位,例如:
#include <bitset>
#include <cstdio>
#include <cassert>
int main()
{
std::bitset<1> aoeu;
int ret, input;
ret = std::scanf( "%d", &input );
assert( ret == 1 );
aoeu[0] = input;
std::printf( "%d\n", static_cast<int>(aoeu[0]) );
}
如果您想知道为什么我不是using namespace std;
,尽管您在问题中这样做了,那么您可能想阅读这个 StackOverflow 问题。