简体   繁体   English

使用 scanf/printf 从 bitset 输入/输出

[英]Using scanf/printf to input into/output from a bitset

I'm somewhat new to C++, and I was wondering how to scanf into or printf out of a bitset, ie, what is the appropriate type specifier for I/O to a bitset index?我对 C++ 有点陌生,我想知道如何从位集中扫描到或 printf,即,对于位集索引的 I/O 合适的类型说明符是什么? An example of what I would want to do is:我想做的一个例子是:

#include <bitset>
#include <stdio.h>

using namespace std; 

int main() 
{
    bitset<1> aoeu;
    scanf("%d" &bitset[0]); //this line
    printf("%d" bitset[0]); // this line
}

As ChrisMM mentioned, you should use the the C++ way of doing input and output.正如 ChrisMM 所提到的,您应该使用 C++ 输入方式和 output。 Luckily, a std::bitset has overloads for operator<< and operator>> so you directly read from std::cin and write to std::cout , without needing to_string() , like so:幸运的是, std::bitset具有operator<<operator>>的重载,因此您可以直接从std::cin读取并写入std::cout ,而不需要to_string() ,如下所示:

#include <bitset>
#include <iostream>
 
int main() 
{
    std::bitset<1> aoeu;
    std::cin >> aoeu; // read bitset from standard input
    std::cout << aoeu; // write bitset to standard output
}

If you just want to read one specific bit and put it in a bitset, you have to do it a bit more indirect:如果您只想读取一个特定位并将其放入位集中,则必须更间接地进行:

std::bitset<3> bits;
int value;
std::cin >> value; // read one integer, assuming it will be 0 or 1
bits[1] = value; // store it in the second bit of the bitset

Your question on how to accomplish this with scanf and printf seems to be an XY problem .您关于如何使用scanfprintf完成此任务的问题似乎是XY 问题 The answer provided by @GSliepen shows you how to do it properly in C++. @GSliepen 提供的答案向您展示了如何在 C++ 中正确执行此操作。

However, in case you are really interested on how to accomplish this using scanf and printf (which I don't recommend), I will give you a direct answer to your question:但是,如果您真的对如何使用scanfprintf (我不推荐)完成此操作感兴趣,我会直接回答您的问题:

You cannot use scanf directly with bitsets, because scanf requires an address to at least a byte, not a single bit.您不能直接将scanf与位集一起使用,因为scanf需要至少一个字节的地址,而不是单个位。 Addresses to single bits don't even exist (at least on most platforms).甚至不存在单个位的地址(至少在大多数平台上)。 However, you can first use scanf to write to a temporary byte ( unsigned char ) or an int and then convert it to a bit for the bitset, for example like this:但是,您可以先使用scanf写入临时字节 ( unsigned char ) 或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]) );
}

In case you are wondering why I am not using namespace std;如果您想知道为什么我不using namespace std; , although you do in your question, then you may want to read this StackOverflow question . ,尽管您在问题中这样做了,但您可能想阅读这个 StackOverflow 问题

To print the value of a specific bit j printf("%d", aoeu(j));打印特定位j的值printf("%d", aoeu(j));

To print the value of the whole bitset I would suggest using要打印整个位集的值,我建议使用

printf("%s\n", val.to_string().c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM