std::bitset, simple question

H

Haro Panosyan

How to construct a bitset from string?

For example:
std::bitset<16> b16("1011011110001011");

Is this possible?

Thanks in advance.
-haro
 
V

Victor Bazarov

Haro said:
How to construct a bitset from string?

For example:
std::bitset<16> b16("1011011110001011");

Is this possible?

Yes. Have you tried RTFMing?
 
H

Haro Panosyan

Victor said:
Yes. Have you tried RTFMing?

I am using g++ 3.3.3 for SunOS. Here is the code:

#include <bitset>

int main()
{
std::bitset<8> b2("10101010");// <- line 5
return 0;
}


And here is the error message:
>g++ bitset1.cpp -o bitset1
bitset1.cpp: In function `int main()':
bitset1.cpp:5: error: invalid conversion from `const char*' to `long
unsigned
int'
bitset1.cpp:5: error: initializing argument 1 of `
std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb = 8]'
 
B

Ben Pope

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
 
B

Ben Pope

Haro said:
How to construct a bitset from string?

For example:
std::bitset<16> b16("1011011110001011");

Is this possible?

Ignore my other post (which refuses to show up for now).

You can certainly construct it from a std::string, but not a const char[17]

I'll leave that up to you :p

Ben Pope
 
H

Haro Panosyan

Victor said:
REALLY?

I just went to www.google.com, typed in "bitset from string" and
the first link I got was 'http://www.sgi.com/tech/stl/bitset.html'
which describes the interface in detail. What web did you search?

Same web, check my other post with compiler error. Linux is same.
I may be missing something, but cannot find what, hence I am here for
help.

Beleive me, not everybody comes here without RTFMing!
( Finally I know what is it)
 
V

Victor Bazarov

Haro said:
Victor said:
Yes. Have you tried RTFMing?


I am using g++ 3.3.3 for SunOS. Here is the code:

#include <bitset>

int main()
{
std::bitset<8> b2("10101010");// <- line 5
return 0;
}


And here is the error message:
g++ bitset1.cpp -o bitset1
bitset1.cpp: In function `int main()':
bitset1.cpp:5: error: invalid conversion from `const char*' to `long
unsigned
int'
bitset1.cpp:5: error: initializing argument 1 of `
std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb = 8]'

Well "10101010" is not really a string, is it? It's a _string_literal_.
The FM says that you should be able to initialise a bitset from
'std::string' (if you did Read The FM, of course). So, a relatively easy
progression from your code to working one should be

std::bitset<8> b2(std::string("10101010"));

[Perhaps I presume too much about people who end up asking here after
trying to solve their problems. My fault. Please forgive me.]
 
P

P.J. Plauger

Victor said:
Yes. Have you tried RTFMing?

I am using g++ 3.3.3 for SunOS. Here is the code:

#include <bitset>

int main()
{
std::bitset<8> b2("10101010");// <- line 5
return 0;
}


And here is the error message:
g++ bitset1.cpp -o bitset1
bitset1.cpp: In function `int main()':
bitset1.cpp:5: error: invalid conversion from `const char*' to `long
unsigned
int'
bitset1.cpp:5: error: initializing argument 1 of `
std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb =
8]'

std::bitset<8> b2(std::string("10101010"));

works for me.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
I

int2str

Haro said:
How to construct a bitset from string?

For example:
std::bitset<16> b16("1011011110001011");

Is this possible?

Thanks in advance.
-haro

#include <bitset>
#include <string>

int main()
{
std::bitset<16> b16( std::string("1011011110001011") );
}

Cheers,
Andre
 
H

Haro Panosyan

Thanks Thomas,

This now works:

#include <bitset>
#include <string>

int main()
{
std::bitset<8> b2(std::string("10101010"));
return 0;
}

-haro
 
H

Haro Panosyan

Thanks Ben,

As pointed by Thomas in this thread, I was using
undefined constructor.

-haro


Ben said:
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
 
H

Haro Panosyan

Victor said:
Haro said:
Victor said:
Haro Panosyan wrote:

How to construct a bitset from string?

For example:
std::bitset<16> b16("1011011110001011");

Is this possible?




Yes. Have you tried RTFMing?



I am using g++ 3.3.3 for SunOS. Here is the code:

#include <bitset>

int main()
{
std::bitset<8> b2("10101010");// <- line 5
return 0;
}


And here is the error message:
g++ bitset1.cpp -o bitset1
bitset1.cpp: In function `int main()':
bitset1.cpp:5: error: invalid conversion from `const char*' to `long
unsigned
int'
bitset1.cpp:5: error: initializing argument 1 of `
std::bitset<_Nb>::bitset(long unsigned int) [with unsigned int _Nb
= 8]'


Well "10101010" is not really a string, is it? It's a _string_literal_.
The FM says that you should be able to initialise a bitset from
'std::string' (if you did Read The FM, of course). So, a relatively easy
progression from your code to working one should be

std::bitset<8> b2(std::string("10101010"));

[Perhaps I presume too much about people who end up asking here after
trying to solve their problems. My fault. Please forgive me.]

No problem, and I am with you.
Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top