C++ bitset头文件中的内容

CLass template

std::bitset

类模板格式template<size_t N> class bitset

  • bitset是用来存储比特的集合。
  • 可以像数组取得数组值地方法获取其中的比特位。
  • 可以从string类型或者整型值构造或者向它们转换。
  • bitset的大小在编译时就已经确定,取决于模板参数。
  • 也可以用vector特化类型实现bitset类似功能

1.成员类型

只有一种类型std::bitset::reference。它是使用操作符[]申请非常对象时返回的一种类型;他用一种类似bool类型的引用来访问其中的bit

class bitset::reference {
  friend class bitset;
  reference();                                        // no public constructor
public:
  ~reference();
  operator bool() const;                              // convert to bool
  reference& operator= (bool x);                      // assign bool
  reference& operator= (const reference& x);          // assign bit
  reference& flip();                                  // flip bit value
  bool operator~() const;                             // return inverse value
}

2.成员函数

1.构造函数

default (1)
bitset();
/*
*默认构造函数,比特位默认设置为0
*/
integer value (2)
bitset (unsigned long val);
/*
 用val的值初始化对象
/*
string (3)
template<class charT, class traits, class Alloc>
  explicit bitset (const basic_string<charT,traits,Alloc>& str,
    typename basic_string<charT,traits,Alloc>::size_type pos = 0,
    typename basic_string<charT,traits,Alloc>::size_type n =
      basic_string<charT,traits,Alloc>::npos);
/*
 用str中的0或者1来初始化bitset对象的前n个比特
*/

bitset有与构造器无关且取决于模板参数N的固定大小,那些没有显示初始化的位会被设置为0;

// constructing bitsets
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<16> foo;
  std::bitset<16> bar (0xfa2);
  std::bitset<16> baz (std::string("0101111001"));

  std::cout << "foo: " << foo << '\n';
  std::cout << "bar: " << bar << '\n';
  std::cout << "baz: " << baz << '\n';

  return 0;
 }


output:
foo: 0000000000000000
bar: 0000111110100010
baz: 0000000101111001
2.操作符

member functions
bitset& operator&= (const bitset& rhs);
bitset& operator|= (const bitset& rhs);
bitset& operator^= (const bitset& rhs);
bitset& operator<<= (size_t pos);
bitset& operator>>= (size_t pos);
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const;
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;



non-member functions
template<size_t N>
  bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs);



iostream inserters/extractors
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs);
//实例:
// bitset operators
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1001"));
  std::bitset<4> bar (std::string("0011"));

  std::cout << (foo^=bar) << '\n';       // 1010 (XOR,assign)
  std::cout << (foo&=bar) << '\n';       // 0010 (AND,assign)
  std::cout << (foo|=bar) << '\n';       // 0011 (OR,assign)

  std::cout << (foo<<=2) << '\n';        // 1100 (SHL,assign)
  std::cout << (foo>>=1) << '\n';        // 0110 (SHR,assign)

  std::cout << (~bar) << '\n';           // 1100 (NOT)
  std::cout << (bar<<1) << '\n';         // 0110 (SHL)
  std::cout << (bar>>1) << '\n';         // 0001 (SHR)

  std::cout << (foo==bar) << '\n';       // false (0110==0011)
  std::cout << (foo!=bar) << '\n';       // true  (0110!=0011)

  std::cout << (foo&bar) << '\n';        // 0010
  std::cout << (foo|bar) << '\n';        // 0111
  std::cout << (foo^bar) << '\n';        // 0101

  return 0;
}
//输出
1010
0010
0011
1100
0110
1100
0110
0001
0
1
0010
0111
0101

3.比特获取

1.操作符[]
// bitset::operator[]
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;

  foo[1]=1;             // 0010
  foo[2]=foo[1];        // 0110

  std::cout << "foo: " << foo << '\n';

  return 0;
}
output:foo: 0110
2.count

被置位的位的个数

size_t count() const;

size()返回是位的总个数

size_t size() const;
3.test
bool test (size_t pos) const;
4.any
bool any() const;
5.all
bool all() const;
6.none

和any正好相反

比特操作

1.set(resets与之相反) reset
all bits (1)
bitset& set();

所有的位都置1


single bit (2)
bitset& set (size_t pos, bool val = true);
 特定位置 置为val 默认为true


all bits (1)
bitset& reset();
所有位复位


single bit (2)
bitset& reset (size_t pos);
 特定位复位
2.flip

all bits (1)
bitset& flip();



single bit (2)
bitset& flip (size_t pos);

3.比特设置操作

1.to_string
template <class charT, class traits, class Alloc>
  basic_string<charT,traits,Alloc> to_string() const;
2.to_ulong
unsigned long to_ulong() const;

//examples
// bitset::to_ulong
#include <iostream>       // std::cout
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo;     // foo: 0000
  foo.set();              // foo: 1111

  std::cout << foo << " as an integer is: " << foo.to_ulong() << '\n';

  return 0;
}
//output:
1111 as an integer is: 15
3.to_ullong

4.非成员函数的重载运算符


member functions
bitset& operator&= (const bitset& rhs);
bitset& operator|= (const bitset& rhs);
bitset& operator^= (const bitset& rhs);
bitset& operator<<= (size_t pos);
bitset& operator>>= (size_t pos);
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const;
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;



non-member functions
template<size_t N>
  bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
  bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs);



iostream inserters/extractors
template<class charT, class traits, size_t N>
  basic_istream<charT, traits>&
    operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
  basic_ostream<charT, traits>&
    operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs);

//examples
// bitset operators
#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<4> foo (std::string("1001"));
  std::bitset<4> bar (std::string("0011"));

  std::cout << (foo^=bar) << '\n';       // 1010 (XOR,assign)
  std::cout << (foo&=bar) << '\n';       // 0010 (AND,assign)
  std::cout << (foo|=bar) << '\n';       // 0011 (OR,assign)

  std::cout << (foo<<=2) << '\n';        // 1100 (SHL,assign)
  std::cout << (foo>>=1) << '\n';        // 0110 (SHR,assign)

  std::cout << (~bar) << '\n';           // 1100 (NOT)
  std::cout << (bar<<1) << '\n';         // 0110 (SHL)
  std::cout << (bar>>1) << '\n';         // 0001 (SHR)

  std::cout << (foo==bar) << '\n';       // false (0110==0011)
  std::cout << (foo!=bar) << '\n';       // true  (0110!=0011)

  std::cout << (foo&bar) << '\n';        // 0010
  std::cout << (foo|bar) << '\n';        // 0111
  std::cout << (foo^bar) << '\n';        // 0101

  return 0;
}
//output:
1010
0010
0011
1100
0110
1100
0110
0001
0
1
0010
0111
0101

5.非成员类模板特例化

template <class T> struct hash; // unspecialized
template <size_t N> struct hash<bitset<N>>;// bitset specialization
成员函数:
operator()
Returns a hash value for its argument, as a value of type size_t.
size_t is an unsigned integral type.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值