STL容器之 bitset
1、STL容器
容器是用来保存其它对象的对象。被保存的对象称为容器的元素。容器一般实现为模板类。
容器为元素分配存储空间,并提供元素访问方法。
stack,queue 和 priority_queue被实现为“容器适配器”。容器适配器并不是一个独立的容器,只是提供特定接口,而其内部使用其它容器来实现。
STL提供了以下容器模板类
1.1、顺序容器
vector:向量
deque:双向队列
list:链表
1.2、容器适配器
stack:栈,先进后出(LIFO)
queue:队列,先进先出(FIFO)
priority_queue:优先级队列
1.3、联合容器
set:集合
multiset:多键集合
map:映射
multimap:多键映射
bitset:位集
2、bitset
前面总体介绍了一些关于STL容器的知识,下面将对bitset进行详细介绍。
2.1、头文件、名字空间和bitset声明
[cpp]
#include <bitset>
using std::bitset;
bitset声明:
[cpp]
template < size_t N > class bitset;
2.2、构造函数
[cpp] view plaincopyprint?
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);
bitset<N>::bitset () 实例化一个具有N位的位集,每位均设为0。
bitset<N>::bitset (unsigned long val) 实例化一个具有N位的位集,位集使用无符号长整型val初始化位集。val的低位对应位集的低位。如果N大于val占用的位数,则多出的高位使用0初始化,低位使用val初始化。如果N小于val占用的位数,则使用val的低N位初始化位集。
第三个构造函数使用二进制字符串str初始化位集。开始于str[pos],使用字符串的n位。如果不指定参数n,则使用到字符串尾。值得注意的一点是,字符串的高位初始化位集的低位。如果用于初始化的字符串子串的长度大于位集位数N,则使用字符串子串的低N字节进行初始化。如果用于初始化的字符串子串的长度小于位集位数N,则使用字符串子串初始化位集的低位,超出位数使用0初始化。
示例代码:
[cpp]
// 32位机上,sizeof (unsigned long) = 4,即32位
// 使用无符号整数初始化位集
unsigned long n = 0xf0f0ff00;
bitset<32> bits1 (n); //位集从高到低:11110000111100001111111100000000
bitset<16> bits2 (n); //位集从高到低:1111111100000000
bitset<40> bits3 (n); //位集从高到低:0000000011110000111100001111111100000000
// 使用字符串初始化位集
string str = "1111111100000000";
bitset<16> strbits1 (str); //位集从高到低:1111111100000000
bitset<10> strbits2 (str); //位集从高到低:1111111100
bitset<20> strbits3 (str); //位集从高到低:00001111111100000000
// 如果使用非二进制串初始化会怎样?
bitset<32> bits (-1); // 会把负数转换为相应的正数来初始化。此处相当于使用0xffffffff初始化。
// 如果使用负整数初始化会怎样?
bitset<32> strbits (string("01abc10")); // 运行时报错
2.3 运算符
[cpp]
bitset<N>& operator&= (const bitset<N>& rhs);
bitset<N>& operator|= (const bitset<N>& rhs);
bitset<N>& operator^= (const bitset<N>& rhs);
bitset<N>& operator<<= (size_t pos);
bitset<N>& operator>>= (size_t pos);
bitset<N> operator~() const;
bitset<N> operator<<(size_t pos) const;
bitset<N> operator>>(size_t pos) const;
bool operator== (const bitset<N>& rhs) const;
bool operator!= (const bitset<N>& rhs) const;
// *** global 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 global functions (extraction/insertion): ***
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, bitset<N>& rhs);
// *** bit access ***
<pre name="code" class="cpp"><pre>bool operator[] ( size_t pos ) const;
reference operator[] ( size_t pos );
2.4、位操作函数
函数 说明
set () 位集置为全1
set (n) 第n位置为1
reset () 位集置为全0
reset (n) 第n位置为0
flip () 位集翻转
2.5、位集操作函数
函数 说明
to_string 位集转换为字符串
to_ulong 位集转换为无符号整数。如果位集size大于ulong的位数,则舍弃位集高位。
count 返回位集中1的个数
size 返回位集的容量
any 如果位集中有1,则返回true
none 如果位集中没有1,则返回true
test (n) 如果第n位为1,则返回true