读书人

bitset的几行程序,一运行就崩溃,为什么

发布时间: 2012-04-24 14:15:38 作者: rapoo

bitset的几行程序,一运行就崩溃,为什么?
程序如下。错在哪里?

C/C++ code
#include<bitset>using namespace std;int main(void){    bitset<20> bs;    bs.set(atol("10101010"));    return 0;}


[解决办法]
bitset<N>& set ( size_t pos, bool val = true );Set bits
The version with no parameters sets (to 1) all the bits in the bitset.
The parameterized version, stores val as the new value for bit at position pos.

好好看看set成员函数第一个参数的意义吧。你将("10101010");转换成long值之后是多大,在看看你的bitset总共多少个位。

[解决办法]
你的bs长度才 20.。 atol ,值是 10101010,,1千万了。
太大了。
这样
bitset<20> bs("10101010");
[解决办法]
++
探讨
bitset<N>& set ( size_t pos, bool val = true );Set bits
The version with no parameters sets (to 1) all the bits in the bitset.
The parameterized version, stores val as the new value for bit at p……

[解决办法]
bitset::set
bitset<N>& set();
bitset<N>& set(size_t pos, bool val = true);
The first member function resets all bits in the bit sequence, then returns *this. The second member function throws out_of_range if size() <= pos. Otherwise, it stores val in the bit at position pos, then returns *this.


[解决办法]
探讨

bitset<N>& set ( size_t pos, bool val = true );Set bits
The version with no parameters sets (to 1) all the bits in the bitset.
The parameterized version, stores val as the new value for bit at ……

[解决办法]
超出范围了~~~~~
[解决办法]
atol("10101010") 设置小一点就没问题了,最好和size()比较一下,不要超出长度
[解决办法]
差点被你蒙过去……明确函数的含义很重要阿!!!

读书人网 >C++

热点推荐