当前头文件是通用工具库(Diagnostics library)中的部分内容。

​通用工具库(参考手册) ​通用工具库(标准)

  • bitset 实现了恒定长度的位数组
    std::hash<std::bitset> C++11 提供了对 bitset 的哈希(Hash)支持
  • operator&
    operator|
    operator^
    对位集(Bitset)进行二进制逻辑(Binary logic)操作
    operator<<
    operator>>
    对位集进行流输入或输出操作
  • #include <string>
    #include <iosfwd> // for istream, ostream
     
    namespace std {
        template <size_t N> class bitset;
     
        // bitset operators:
        template <size_t N>
            bitset<N> operator&(const bitset<N>&, const bitset<N>&) noexcept;
        template <size_t N>
            bitset<N> operator|(const bitset<N>&, const bitset<N>&) noexcept;
        template <size_t N>
            bitset<N> operator^(const bitset<N>&, const bitset<N>&) noexcept;
        template <class charT, class traits, size_t N>
            basic_istream<charT, traits>&
            operator>>(basic_istream<charT, traits>& is, bitset<N>& x);
        template <class charT, class traits, size_t N>
            basic_ostream<charT, traits>&
            operator<<(basic_ostream<charT, traits>& os, const bitset<N>& x);
     
        // Hash support
        template <class T> struct hash;
        template <size_t N> struct hash<bitset<N> >;
     
    }
  • template<size_t N> class bitset {
    public:
        // bit reference:
        class reference {
            friend class bitset;
            reference() noexcept;
        public:
            ~reference() noexcept;
            reference& operator=(bool x) noexcept;          //  for b[i] = x;
            reference& operator=(const reference&) noexcept;//  for b[i] = b[j];
            bool operator~() const noexcept;                //  flips the bit
            operator bool() const noexcept;                 //  for x = b[i];
            reference& flip() noexcept;                     //  for b[i].flip();
     
        };
     
        //constructors:
        constexpr bitset() noexcept;
        constexpr bitset(unsigned long long val) noexcept;
        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,
                charT zero = charT(’0’), charT one = charT(’1’));
        template <class charT>
            explicit bitset(
                const charT* str,
                typename basic_string<charT>::size_type n = basic_string<charT>::npos,
                charT zero = charT(’0’), charT one = charT(’1’));
     
        // bitset operations:
        bitset<N>& operator&=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator|=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator^=(const bitset<N>& rhs) noexcept;
        bitset<N>& operator<<=(size_t pos) noexcept;
        bitset<N>& operator>>=(size_t pos) noexcept;
        bitset<N>& set() noexcept;
        bitset<N>& set(size_t pos, bool val = true);
        bitset<N>& reset() noexcept;
        bitset<N>& reset(size_t pos);
        bitset<N> operator~() const noexcept;
        bitset<N>& flip() noexcept;
        bitset<N>& flip(size_t pos);
     
        // element access:
        constexpr bool operator[](size_t pos) const; // for b[i];
        reference operator[](size_t pos); // for b[i];
     
     
        unsigned long to_ulong() const;
        unsigned long long to_ullong() const;
        template <class charT = char,
                class traits = char_traits<charT>,
                class Allocator = allocator<charT> >
            basic_string<charT, traits, Allocator>
            to_string(charT zero = charT(’0’), charT one = charT(’1’)) const;
     
        size_t count() const noexcept;
        constexpr size_t size() noexcept;
     
        bool operator==(const bitset<N>& rhs) const noexcept;
        bool operator!=(const bitset<N>& rhs) const noexcept;
     
        bool test(size_t pos) const;
        bool all() const noexcept;
        bool any() const noexcept;
        bool none() const noexcept;
     
        bitset<N> operator<<(size_t pos) const noexcept;
        bitset<N> operator>>(size_t pos) const noexcept;
    };