标准非STL之bitset

template <size_t N> class bitset;

Bitset
A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).
[bitset存储位(元素只能为两种可能的数值,即0或1,true或false,...)]
The class emulates an array of bool elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char).
[bitset类似于存储bool型元素的数组,但会优化空间配置。一般来说,bitset中的每一个元素都只占一位(在大多数系统下,bitset中每一个元素所占位数都是最小数据类型char所占位数的1/8)]
Each bit position can be accessed individually: for example, for a given bitset named foo, the expression foo[3] accesses its fourth bit, just like a regular array accesses its elements. But because no elemental type is a single bit in most C++ environments, the individual elements are accessed as special references type (see bitset::reference).
[bitset中的每一位都可以被单独读取,举个例子,有一个名为foo的bitset,则表达式foo[3]就像一个普通数组获取其元素那样,表示获取foo的第4位]
Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and members to_ulong and to_string). They can also be directly inserted and extracted from streams in binary format (see applicable operators).
[bitset可以通过整数值或者二进制字符串构造,也可以转换为整数值或者二进制字符串(参见bitset的构造函数及其成员函数to_ulong和to_string)。bitset也可以直接插入或者读取自二进制格式的流中(参见applicable operators)]
The size of a bitset is fixed at compile-time (determined by its template parameter). For a class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).
[bitset的长度是在编译期间确定的(由它的模板参数决定),如果想要使用一个可以在运行期间再确定长度且也能够优化空间配置的类,请参见专门为bool设计的类vecotr<bool>]

/*
bitset();

bitset (unsigned long val);

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);

Construct bitset
Constructs a bitset container object:
[构造一个bitset容器对象]
(1) default constructor
The object is initialized with zeros.
[默认构造函数:对象初始化为0]
(2) initialization from integer value
Initializes the object with the bit values of val:
[用整数值初始化]
(3) initialization from string
Uses the sequence of zeros and/or ones in str to initialize the first n bit positions of the constructed bitset object.
[用str中前n个0或1来构造bitset对象]

Note that bitset objects have a fixed size (determined by their class template argument) no matter the constructor used: Those bit positions not explicitly set by the constructor are initialized with a value of zero.
[需要注意的是,bitset对象的长度在编译时就已经确定,如果构造bitset时没有显示地指定相应bit的值,则对应bit初始化为0]

val
Integral value whose bits are copied to the bitset positions.
- If the value representation of val is greater than the bitset size, only the least significant bits of val are taken into consideration.
[如果val的长度比bitset的长度大,则只考虑val的最低有效位(即顶端被截除)]
- If the value representation of val is less than the bitset size, the remaining bit positions are initialized to zero.
[如果val的长度比bitset的长度小,则剩余的位被初始化为0]

str
A basic_string whose contents are used to initialize the bitset:
The constructor parses the string reading at most n characters beginning at pos, interpreting the character values '0' and '1' as zero and one, respectively.
[构造函数读取string中从pos开始的n个(至多n个)字符用来初始化bitset]
If this sequence is shorter than the bitset size, the remaining bit positions are initialized to zero.
[如果string的长度比bitset的长度小,则剩余的位数被初始化为0]
*/

#include <iostream>
#include <string>
#include <bitset>

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

    std::cout<<"foo: "<<foo<<'\n';        //00000000
    std::cout<<"bar: "<<bar<<'\n';        //10100010, the least significant bits of val are taken into consideration
    std::cout<<"baz: "<<baz<<'\n';        //01011110,  reading at most n characters beginning at pos

    system("pause");
    return 0;
}
/*
std::bitset operators

// 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);
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) 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/extracto
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
Performs the proper bitwise operation using the contents of the bitset.
[按位操作bitset]
*/

#include <iostream>
#include <string>
#include <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<<std::boolalpha<<(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';         //0101
    std::cout<<(foo|bar)<<'\n';         //0111

    system("pause");
    return 0;
}
/*
bool none() const;                  //检查二进制位是否全部为0,是则返回真
bool any() const;                   //检查二进制位是否有1,有则返回真
size_t count() const;               //计算bitset中为1的位的个数
size_t size() const;                //返回bitset的位数
bool test(size_t pos) const;        //返回bitset中pos位上的值
reference operator[] (size_t pos)   //返回bitset中pos位上的值的引用

bitset& flip();
bitset& flip(size_t pos);        //反置bitset中所有的位,如果指定pos,那么只有pos上的位被反置

bitset& reset()
bitset& reset(size_t pos);      //重置二进制位(全部设置为0),如果指定pos,那么只有pos上的位被重置

bitset& set();
bitset& set(size_t pos, bool val = true);    //设置二进制位(全部设置为1),如果指定pos,那么只有pos上的位被设置为v

to_string();       //返回bitset的string形式
to_ulong();        //返回bitset的unsigned long形式
*/

#include <iostream>
#include <string>
#include <bitset>

int main()
{
    std::bitset<4> mybits;
    mybits.set();

    std::string mystring = mybits.to_string();
    std::cout<<"mystring: "<<mystring<<'\n';          //1111

    unsigned long myulong = mybits.to_ulong();
    std::cout<<"myulong: "<<myulong<<'\n';            //15

    system("pause");
    return 0;
}
posted @ 2015-06-07 16:01  codeplayplus  阅读(316)  评论(0编辑  收藏  举报