Namespaces
Variants

std::bitset<N>:: operator==, std::bitset<N>:: operator!=

From cppreference.net
Utilities library
bool operator == ( const bitset & rhs ) const ;
(1) (自 C++11 起为 noexcept)
(自 C++23 起为 constexpr)
bool operator ! = ( const bitset & rhs ) const ;
(2) (自 C++11 起为 noexcept)
(直至 C++20)
1) * this rhs 中的所有位都相等时返回 true。
2) * this rhs 中存在任意不相等位,则返回 true。

!= 运算符由 operator== 合成生成

(since C++20)

参数

rhs - 要比较的位集

返回值

1) true * this 中每个位的值等于 rhs 中对应位的值时成立,否则为 false
2) true 如果 ! ( * this == rhs ) ,否则为 false

示例

比较给定的位集以确定它们是否相同:

#include <bitset>
#include <iostream>
int main()
{
    std::bitset<4> b1(0b0011);
    std::bitset<4> b2(b1);
    std::bitset<4> b3(0b0100);
    std::cout << std::boolalpha;
    std::cout << "b1 == b2: " << (b1 == b2) << '\n';
    std::cout << "b1 == b3: " << (b1 == b3) << '\n';
    std::cout << "b1 != b3: " << (b1 != b3) << '\n';
//  b1 == std::bitset<3>{}; // compile-time error: incompatible types
}

输出:

b1 == b2: true
b1 == b3: false
b1 != b3: true