关于bitset的一个小问题

snsn1984
博客专家认证
2008-10-29 10:07:05
#include<iostream>
#include<bitset>

using namespace std;

int main()
{
bitset<32> bits;
int x=1,y=0,z=0;
while(z<=21)
{
bits.set(z);
y=x;
x=z;
z=x+y;

}
cout<<"bits: "<<bits<<endl;
return 0;
}
我这样的话,调试运行都正常,但是下边的方法调试没有错误,运行报错.
#include<iostream>
#include<bitset>

using namespace std;

int main()
{
bitset<32> bits;
int x=1,y=0,z=0;
while(z<=21)
{

y=x;
x=z;
z=x+y;
bits.set(z);
}
cout<<"bits: "<<bits<<endl;
return 0;
}
运行报的错误信息:Debug Error!
请教为什么会出现这样的情况?顺序不同为什么会导致完全不同的两种情况?先谢谢大家.
...全文
131 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
snsn1984 2008-10-29
  • 打赏
  • 举报
回复
这个分,分的不太均匀,感谢每个兄弟,希望大家别介意哦.
snsn1984 2008-10-29
  • 打赏
  • 举报
回复
谢谢大家,我把2)中的z<=21改成了21,就可以正常执行了.
太乙 2008-10-29
  • 打赏
  • 举报
回复


#include <iostream>
#include <bitset>

using namespace std;

int main()
{
bitset <32> bits;
int x=1,y=0,z=0;
while(z <=21)
{

y=x;
x=z;
z=x+y;
bits.set(z);
}
cout <<"bits: " <<bits <<endl;
return 0;
}



因为z==21后,进入while循环,z=34,而对于bits.set(34)肯定会报错

而如果把bits.set(z)放在开头,那么z就不会出现=34的情况!!

z = 34 是因为z=x+y引起的@






帅得不敢出门 2008-10-29
  • 打赏
  • 举报
回复
看输出最清楚了.

#include <iostream>
#include <bitset>

using namespace std;

int main()
{
bitset <32> bits;
int x=1,y=0,z=0;
while(z <=21)
{

y=x;
x=z;
z=x+y;
cout << "Z" << z << endl;
bits.set(z);
cout << "bits: " <<bits <<endl;
}

return 0;
}

Z1
bits: 00000000000000000000000000000010
Z1
bits: 00000000000000000000000000000010
Z2
bits: 00000000000000000000000000000110
Z3
bits: 00000000000000000000000000001110
Z5
bits: 00000000000000000000000000101110
Z8
bits: 00000000000000000000000100101110
Z13
bits: 00000000000000000010000100101110
Z21
bits: 00000000001000000010000100101110
Z34
Press any key to continue
Jncryang 2008-10-29
  • 打赏
  • 举报
回复
第二个程序是Z运行完结果后在赋值,执行最后一次循环时,Z值经x+Y计算后得34,超过了32最大值,因此出错!
sys0004 2008-10-29
  • 打赏
  • 举报
回复
bitset <32> bits; 设置了最大位数为32
因为1)中你的while(z<=21)保证了z不会大于32,所以没问题
2)中因为在while(z<=21)做了y=x;x=z;z=x+y;导致最后一次z的值大于32越界所以出错了。
bitxinhai 2008-10-29
  • 打赏
  • 举报
回复
y=x;
x=z;
z=x+y;
bits.set(z);//当z>32时,bitset会出现运行时错误!!应该首先判断z是否大于32
fox000002 2008-10-29
  • 打赏
  • 举报
回复
差别就是,后者在执行到 z=34 的时候继续执行 bits.set(z); 这就报错,来不及判断 z <=21

前者执行到 z=34 后,判断 z <=21 就退出了

在C++的STL中实现由一个bitset类模板,其用法如下: std::bitset bs; 也就是说,这个bs只能支持64位以内的位存储和操作;bs一旦定义就不能动态增长了。本资源附件中实现了一个动态Bitset,和标准bitset兼容。 /** @defgroup Bitset Bitset位集类 * @{ */ //根据std::bitset改写,函数意义和std::bitset保持一致 class CORE_API Bitset: public Serializable { typedef typename uint32_t _Ty; static const int _Bitsperword = (CHAR_BIT * sizeof(_Ty)); _Ty * _Array; //最低位放在[0]位置,每位的默认值为0 int _Bits;//最大有效的Bit个数 private: int calculateWords()const; void tidy(_Ty _Wordval = 0); void trim(); _Ty getWord(size_t _Wpos)const; public: //默认构造 Bitset(); //传入最大的位数,每位默认是0 Bitset(int nBits); virtual ~Bitset(); //直接整数转化成二进制,赋值给Bitset,最高低放在[0]位置 Bitset(unsigned long long _Val); //拷贝构造函数 Bitset(const Bitset & b); Bitset(const char * str); Bitset(const std::string & str, size_t _Pos, size_t _Count); public: size_t size()const; //返回设置为1的位数 size_t count() const; bool subscript(size_t _Pos) const; bool get(size_t pos) const; //设置指定位置为0或1,true表示1,false表示0,如果pos大于数组长度,则自动扩展 void set(size_t _Pos, bool _Val = true); //将位数组转换成整数,最低位放在[0]位置 //例如数组中存放的1011,则返回13,而不是返回11 unsigned long long to_ullong() const; bool test(size_t _Pos) const; bool any() const; bool none() const; bool all() const; std::string to_string() const; public: //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (const Bitset& b); //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (unsigned long long ull); //返回指定位置的值,如果pos大于位数组长度,自动拓展 bool operator [] (const size_t pos); //测试两个Bitset是否相等 bool operator == (const Bitset & b); bool operator != (const Bitset & b); Bitset operator<>(size_t _Pos) const; bool operator > (const Bitset & c)const; bool operator < (const Bitset & c)const; Bitset& operator &=(const Bitset& _Right); Bitset& operator|=(const Bitset& _Right); Bitset& operator^=(const Bitset& _Right); Bitset& operator<>=(size_t _Pos); public: Bitset& flip(size_t _Pos); Bitset& flip(); //将高位与低位互相,如数组存放的是1011,则本函数执行后为1101 Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是返回开始位置 size_t find (const Bitset & b) const; size_t find(unsigned long long & b) const; size_t find(const char * b) const; size_t find(const std::string & b) const; //判断本类的位数组是否是b的前缀 bool is_prefix(unsigned long long & b) const; bool is_prefix(const char * b) const; bool is_prefix(const std::string & b) const; bool is_prefix(const Bitset & b) const; void clear(); void resize(size_t newSize); void reset(const unsigned char * flags, size_t s); void reset(unsigned long long _Val); void reset(const char * _Str); void reset(const std::string & _Str, size_t _Pos, size_t _Count); //左移动n位,返回新的Bitset //extendBits=false "1101" 左移动2位 "0100"; //extendBits=true "1101" 左移动2位 "110100"; Bitset leftShift(size_t n,bool extendBits=false)const; //右移动n位,返回新的Bitset //extendBits=false "1101" 右移动2位 "0011"; //extendBits=true "1101" 右移动2位 "001101"; Bitset rightShift(size_t n, bool extendBits = false) const; public: virtual uint32_t getByteArraySize(); // returns the size of the required byte array. virtual void loadFromByteArray(const unsigned char * data); // load this object using the byte array. virtual void storeToByteArray(unsigned char ** data, uint32_t& length) ; // store this object in the byte array. };

64,266

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧