c++11 标准模板(STL)(std::bitset)(七)

定义于头文件 <bitset>

template< std::size_t N >
class bitset;

类模板 bitset 表示一个 N 位的固定大小序列。可以用标准逻辑运算符操作位集,并将它与字符串和整数相互转换。

bitset 满足可复制构造 (CopyConstructible) 及可复制赋值 (CopyAssignable) 的要求。

模板形参

N - 要为 bitset 分配存储的位数

成员类型

reference

表示到一个位的引用的代理类
(类)

成员函数

比较其内容

operator==,!=(std::bitset)

bool operator==( const bitset<N>& rhs ) const;

(1)

bool operator!=( const bitset<N>& rhs ) const;

(2)

 1) 若 *thisrhs 中的所有位相等则返回 true 。

2) 若 *thisrhs 中的任何位不相等则返回 true 。

参数

rhs - 要比较的 bitset

返回值

1) 若 *this 中每位都等于 rhs 中对应位的值则为 true ,否则为 false

2) 若 !(*this == rhs) 则为 true ,否则为 false

容量

返回位集能保有的位数大小

std::bitset<N>::size

std::size_t size() const;

(C++11 前)

constexpr std::size_t size() const noexcept;

(C++11 起)

返回 bitset 所能保有的位数。

参数

(无)

返回值

bitset 所能保有的位数,即模板形参 N

转换

返回数据的字符串表示

std::bitset<N>::to_string
template<

    class CharT,
    class Traits,
    class Alloc

> std::basic_string<CharT,Traits,Allocator> to_string() const;
(C++11 前)
template<

    class CharT = char,
    class Traits = std::char_traits<CharT>,
    class Allocator = std::allocator<CharT>
> std::basic_string<CharT,Traits,Allocator>

    to_string(CharT zero = CharT('0'), CharT one = CharT('1')) const;
(C++11 起)

 转换 bitset 的内容为 string 。用 zero 表示拥有值 false 的位,以 one 表示拥有值 true 的位。

产生的字符串含 N 个字符,其首字符对应最末(第 N-1th )位而其尾字符对应首位。

参数

zero - 用于表示 false 的字符
one - 用于表示 true 的字符

返回值

转换出的字符串

异常

可能从 std::string 构造函数抛出 std::bad_alloc 。

返回数据的 unsigned long 整数表示

std::bitset<N>::to_ulong

unsigned long to_ulong() const

转换 bitset 的内容为 unsigned long 整数。

bitset 的首位对应数的最低位,而尾位对应最高位。

参数

(无)

返回值

转换出的整数

异常

若值不能以 unsigned long 表示则抛出 std::overflow_error 。

返回数据的 unsigned long long 整数表示

std::bitset<N>::to_ullong

unsigned long long to_ullong() const

(C++11 起)

转换 bitset 的内容为 unsigned long long 整数。

bitset 的首位对应数的最低位,而尾位对应最高位。

参数

(无)

返回值

转换出的整数

异常

若值不能以 unsigned long long 表示则为 std::overflow_error 。

辅助类

std::bitset 的散列支持

std::hash (std::bitset)

template<size_t N> struct hash<bitset<N>>;

(C++11 起)

 std::hash 对 std::bitset<N> 的模板特化允许用户获得 std::bitset<N> 类型的对象的哈希。

调用示例

#include <iostream>
#include <bitset>
#include <string>
#include <sstream>
#include <limits>

template<size_t _Nb>
void printBitset(const std::string &name, const std::bitset<_Nb> &bitset)
{
    std::cout << name << ":  ";
    for (size_t index = 0; index < bitset.size(); index++)
    {
        std::cout << bitset[index] << " ";
    }
    std::cout << std::endl;
}

int main()
{
    std::cout << std::boolalpha;

    //转换 bitset 的内容为 string 。用 zero 表示拥有值 false 的位,以 one 表示拥有值 true 的位。
    //产生的字符串含 N 个字符,其首字符对应最末(第 N-1th )位而其尾字符对应首位。
    std::bitset<8> bitset1(123);
    std::cout << "bitset1.to_string():          " << bitset1.to_string() << std::endl;
    std::cout << "bitset1.to_string('*'):       " << bitset1.to_string('*') << std::endl;
    std::cout << "bitset1.to_string('X','Y'):   " << bitset1.to_string('X', 'Y') << std::endl;
    std::cout << std::endl;


    //转换 bitset 的内容为 unsigned long 整数。
    //bitset 的首位对应数的最低位,而尾位对应最高位。
    for (unsigned long i = 0; i < 8; ++i)
    {
        std::bitset<5> b(i);
        std::bitset<5> b_inverted = ~b;
        std::cout << i << '\t';
        std::cout << b << '\t';
        std::cout << b_inverted << '\t';
        std::cout << b_inverted.to_ulong() << '\n';
    }
    std::cout << std::endl;


    //转换 bitset 的内容为 unsigned long long 整数。
    //bitset 的首位对应数的最低位,而尾位对应最高位。
    std::bitset<std::numeric_limits<unsigned long long>::digits> bitset3(
        0x123456789abcdef0LL
    );
    std::cout << bitset3 << "  " << std::hex << bitset3.to_ullong() << std::endl;
    bitset3.flip();
    std::cout << bitset3 << "  " << bitset3.to_ullong() << std::endl;
    std::cout << std::endl;


    //1) 若 *this 与 rhs 中的所有位相等则返回 true 。
    std::bitset<8> bitset5("10101010");
    std::bitset<8> bitset6(bitset5);
    std::bitset<8> bitset7("01010101");
    std::cout << "bitset5:              " << bitset5 << std::endl;
    std::cout << "bitset6:              " << bitset6 << std::endl;
    std::cout << "bitset7:              " << bitset7 << std::endl;
    //1) 若 *this 与 rhs 中的所有位相等则返回 true 。
    //2) 若 *this 与 rhs 中的任何位不相等则返回 true 。
    std::cout << "bitset5 == bitset6    " << (bitset5 == bitset6) << std::endl;
    std::cout << "bitset5 != bitset6    " << (bitset5 != bitset6) << std::endl;
    std::cout << "bitset5 == bitset7    " << (bitset5 == bitset7) << std::endl;
    std::cout << "bitset5 != bitset7    " << (bitset5 != bitset7) << std::endl;
    std::cout << std::endl;

    //返回 bitset 所能保有的位数。
    std::cout << "bitset5.size():       " << bitset5.size() << std::endl;
    std::cout << "bitset6.size():       " << bitset6.size() << std::endl;
    std::cout << "bitset7.size():       " << bitset7.size() << std::endl;
    std::cout << std::endl;

    //std::hash 对 std::bitset<N> 的模板特化允许用户获得 std::bitset<N> 类型的对象的哈希。
    std::hash<std::bitset<8>> hash_fn;
    size_t hash1 = hash_fn(bitset5);
    size_t hash2 = hash_fn(bitset6);
    size_t hash3 = hash_fn(bitset7);
    std::cout << "std::hash<bitset5>:   " << hash1 << std::endl;
    std::cout << "std::hash<bitset6>:   " << hash2 << std::endl;
    std::cout << "std::hash<bitset7>:   " << hash3 << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/131038188
今日推荐