Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::bitset, simple question (http://www.velocityreviews.com/forums/t450330-std-bitset-simple-question.html)

Haro Panosyan 12-06-2005 09:34 PM

std::bitset, simple question
 
How to construct a bitset from string?

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

Is this possible?

Thanks in advance.
-haro

Victor Bazarov 12-06-2005 09:50 PM

Re: std::bitset, simple question
 
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?

Haro Panosyan 12-06-2005 10:22 PM

Re: std::bitset, simple question
 
Victor Bazarov wrote:
> 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?


If RTFMing is about searching the web,
then yes, I did(because I knew you were going to ask:) )





Victor Bazarov 12-06-2005 10:44 PM

Re: std::bitset, simple question
 
Haro Panosyan wrote:
> Victor Bazarov wrote:
>
>> 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?

>
>
> If RTFMing is about searching the web,
> then yes, I did(because I knew you were going to ask:) )


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?

Haro Panosyan 12-06-2005 10:46 PM

Re: std::bitset, simple question
 
Victor Bazarov wrote:
> 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]'

Ben Pope 12-06-2005 10:49 PM

Re: std::bitset, simple question
 
Haro Panosyan wrote:
> 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[i] = str[length - i - 1] == '1';
}

std::cout << bs.to_ulong() << std::endl;
}


Ben Pope
--
I'm not just a number. To many, I'm known as a string...

Ben Pope 12-06-2005 10:55 PM

Re: std::bitset, simple question
 
Haro Panosyan wrote:
> 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
--
I'm not just a number. To many, I'm known as a string...

Haro Panosyan 12-06-2005 10:59 PM

Re: std::bitset, simple question
 
Victor Bazarov wrote:
> Haro Panosyan wrote:
>
>> Victor Bazarov wrote:
>>
>>> 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?

>>
>>
>>
>> If RTFMing is about searching the web,
>> then yes, I did(because I knew you were going to ask:) )

>
>
> 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)

Victor Bazarov 12-06-2005 11:03 PM

Re: std::bitset, simple question
 
Haro Panosyan wrote:
> Victor Bazarov wrote:
>
>> 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.]

Thomas J. Gritzan 12-06-2005 11:05 PM

Re: std::bitset, simple question
 
Haro Panosyan schrieb:
> Victor Bazarov wrote:
>
>> 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?

>
> If RTFMing is about searching the web,
> then yes, I did(because I knew you were going to ask:) )


RTFM is more about reading the manual:
http://www.google.com/search?q=define%3Artfm

But searching the web would do it:
http://www.google.com/search?q=std+bitset

There you could see that std::bitset<> has a constructor from std::string.

Thomas


All times are GMT. The time now is 01:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.