I'm somewhat new to C++, and I was wondering how to scanf into or printf out of a bitset, i.e., what is the appropriate type specifier for I/O to a bitset index? 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. Luckily, a
std::bitsethas overloads foroperator<<andoperator>>so you directly read fromstd::cinand write tostd::cout, without needingto_string(), like so:If you just want to read one specific bit and put it in a bitset, you have to do it a bit more indirect: