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

定义于头文件 <bitset>

template< std::size_t N >
class bitset;

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

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

模板形参

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

成员类型

reference

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

非成员函数

在 bitset 上执行二元逻辑操作

operator&,|,^(std::bitset)

template< std::size_t N >
bitset<N> operator&( const bitset<N>& lhs, const bitset<N>& rhs );

(1) (C++11 前)

template< std::size_t N >
bitset<N> operator&( const bitset<N>& lhs, const bitset<N>& rhs ) noexcept;

(C++11 起)

template< std::size_t N >
bitset<N> operator|( const bitset<N>& lhs, const bitset<N>& rhs );

(2) (C++11 前)

template< std::size_t N >
bitset<N> operator|( const bitset<N>& lhs, const bitset<N>& rhs ) noexcept;

(C++11 起)

template< std::size_t N >
bitset<N> operator^( const bitset<N>& lhs, const bitset<N>& rhs );

(3) (C++11 前)

template< std::size_t N >
bitset<N> operator^( const bitset<N>& lhs, const bitset<N>& rhs ) noexcept;

(C++11 起)

 进行二个 bitset lhsrhs 间的二进制与、或及异或。

1) 返回含 lhsrhs 的位对应对上的二进制与结果的 bitset<N>

2) 返回含 lhsrhs 的位对应对上的二进制或结果的 bitset<N>

3) 返回含 lhsrhs 的位对应对上的二进制异或结果的 bitset<N>

参数

lhs - 运算符左侧的 bitset
rhs - 运算符右侧的 bitset

返回值

1) bitset<N>(lhs) &= rhs

2) bitset<N>(lhs) |= rhs

3) bitset<N>(lhs) ^= rhs

执行 bitset 的流输入和输出

operator<<,>>(std::bitset)
template <class CharT, class Traits, size_t N>

std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,

                                              const bitset<N>& x);
(1)
template <class CharT, class Traits, size_t N>

std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,

                                              bitset<N>& x);
(2)

从字符流插入或释出 bitset 。

1) 写 bitset x 入字符流 os ,如同首先用 to_string() 将它转换成 basic_string<CharT,Traits> ,再用 operator<< (对字符串是有格式输出函数 (FormattedOutputFunction) )将它写入 os 。为一与零使用的字符,通过以 '1' 和 '0' 为参数调用 std::use_facet<std::ctype<CharT>(os.getloc()).widen() 从当前感染的本地环境获得。

2) 表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,这可能跳过前导空白符,之后从 is 释出至多 N 个字符,并存储字符于 bitset x

释出字符直至

  • 已读取 N 个字符
  • 文件尾发生于 is ,或
  • 下个字符既非 is.widen('0') 亦非 is.widen('1')

N > 0 且未释出字符,则调用 is.setstate(ios_base::failbit)

参数

os - 要写入的字符流
is - 要读取的字符流
x - 要读取或写入的 bitset

返回值

操作于上的字符流,例如 osis

调用示例

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

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 lhs 和 rhs 间的二进制与、或及异或。
    std::bitset<8> bitset1("101010");
    std::bitset<8> bitset2("100011");
    std::cout << "bitset1:              " << bitset1 << std::endl;
    std::cout << "bitset2:              " << bitset2 << std::endl;
    //1) 返回含 lhs 和 rhs 的位对应对上的二进制与结果的 bitset<N> 。
    std::cout << "bitset1 & bitset2:    " << (bitset1 & bitset2) << std::endl;
    //2) 返回含 lhs 和 rhs 的位对应对上的二进制或结果的 bitset<N> 。
    std::cout << "bitset1 | bitset2:    " << (bitset1 | bitset2) << std::endl;
    //3) 返回含 lhs 和 rhs 的位对应对上的二进制异或结果的 bitset<N> 。
    std::cout << "bitset1 ^ bitset2:    " << (bitset1 ^ bitset2) << std::endl;
    std::cout << std::endl;

    //从字符流插入或释出 bitset 。
    std::string bit_string = "001101";
    std::istringstream bit_stream(bit_string);
    //1) 写 bitset x 入字符流 os ,如同首先用 to_string() 将它转换成 basic_string<CharT,Traits> ,
    //再用 operator<< (对字符串是有格式输出函数 (FormattedOutputFunction) )将它写入 os 。
    std::bitset<3> bitset3;
    bit_stream >> bitset3; // 读取 "001" ,流仍保有 "101"
    std::cout << "bitset3:              " << bitset3 << std::endl;
    //2) 表现为有格式输入函数 (FormattedInputFunction) 。
    //构造并检查 sentry 对象,这可能跳过前导空白符,之后从 is 释出至多 N 个字符,并存储字符于 bitset x 。
    std::bitset<8> bitset4;
    bit_stream >> bitset4; // 读取 "101" ,产出 8 位集为 "00000101"
    std::cout << "bitset4:              " << bitset4 << std::endl;

    return 0;
}

输出

猜你喜欢

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