C++ primer 习题练习:6.24

本文展示了一个使用 C++ 的 bitset 类进行位操作的例子,重点关注如何将位集转换为 unsigned long 类型,并通过 try-catch 结构来处理可能发生的运行时错误。

#include<iostream> #include<bitset> #include<stdexcept> //vc中不要这个也能通过 using namespace std; int main() { bitset<100> bs; for(size_t ix = 0; ix != bs.size(); ++ix) { bs[ix] = 1; } try { bs.to_ulong();//将bs转化为unsigned long 类型 } catch(runtime_error err) { cout << "err!!!" << endl; cout << err.what() << endl; } return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值