C++语法基础--标准库类型--bitset

1.bitset
   *定义原型:
       template< std::size_t N >
          class bitset;

  
   *构造函数
     原型:
            bitset ( );
            bitset ( unsigned long val );
   template<class charT, class traits, class Allocator>
   explicit bitset ( const basic_string<charT,traits,Allocator>& str,
                     typename basic_string<charT,traits,Allocator>::size_type pos = 0,
                     typename basic_string<charT,traits,Allocator>::size_type n =
                     basic_string<charT,traits,Allocator>::npos);

    解析:
       val:用于初始化的bitset的unsigned long型数


       str:字符串,用于初始化bitset的


       pos:开始偏移str


       n:从str要使用的字符数
       
      eg:
         bitset<4> bst1;  
// 0000
         bitset<4> bst2 (3ul);      // 0011
         bitset<4> bst3 (string("111011")); // 1110
bitset<4> bst4 (string("11101"),1); // 1101,从下标为1出开始取,至到最后
bitset<4> bst5 (string("11101"),1,3); // 0110,从下标为1出开始取,至到下标3(包括),最后高位补0



2.bitset常用操作:

       biset<32>  b(0xfff);

            

   
   其中count,size返回的类型为size_t(,在cstddef头文件有定义,是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。)
      原型:
           size_t count() const;
           size_t size() const;



   eg:
        string str("1100");
        bitset<32> bst(str);
        bitset<4> bst1(0xf);
//1111
cout<<bst.any()<<endl;  //1
cout<<bst.none()<<endl; //0
cout<<bst.count()<<endl;//2
        cout<<bst1.to_ullong()<<endl; //15








      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值