Haro said:
How to construct a bitset from string?
For example:
std::bitset<16> b16("1011011110001011");
Is this possible?
I don't think so.
I was thinking stringstreams, but there is no base 2 manipulator like
oct, hex, dec.
Then I thought std::transform, but bitset has no iterators!
So, presumably you have to do it the hard way:
#include <string>
#include <iostream>
#include <bitset>
#include <algorithm>
int main(int argc, char* argv[])
{
std::string str("1011011110001011");
std::bitset<16> bs;
size_t length = std::min(str.length(), bs.size());
for (size_t i = 0; i < length; ++i) {
bs
= str[length - i - 1] == '1';
}
std::cout << bs.to_ulong() << std::endl;
}
Ben Pope