命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 工具库‎ | bitset
 
 
 
 
bool operator==( const bitset& rhs ) const;
(1) (C++11 起无异常抛出)
(C++23 起为 constexpr)
bool operator!=( const bitset& rhs ) const;
(2) (C++11 起无异常抛出)
(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>{}; // compile-time error: incompatible types
}

输出

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