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

来自cppreference.com
< cpp‎ | utility‎ | bitset
 
 
 
 
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)*thisrhs 中的所有位都相等则返回 true
2)*thisrhs 中有任何位不相等则返回 true

!= 运算符从 operator== 运算符合成

(C++20 起)

[编辑] 参数

rhs - 要比较的 bitset

[编辑] 返回值

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

[编辑] 示例

比较两个 bitset 以确定它们是否等同:

#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>{}; // 编译时错误:不兼容类型
}

输出:

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