std::bitset<N>::operator==, std::bitset<N>::operator!=
来自 zh.cppreference.net
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。|
|
(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