std::bitset<N>::operator==, std::bitset<N>::operator!=
来自 cppreference.cn
bool operator==( const bitset& rhs ) const; |
(1) | (noexcept since C++11) (自 C++11 起为 noexcept) (constexpr since C++23) (自 C++23 起为 constexpr) |
bool operator!=( const bitset& rhs ) const; |
(2) | (noexcept since C++11) (自 C++11 起为 noexcept) (until C++20) (直到 C++20) |
1) 如果 *this 和 rhs 中的所有位都相等,则返回 true。
2) 如果 *this 和 rhs 中的任何位不相等,则返回 true。
(since C++20) (自 C++20 起) |
[edit] 参数
rhs | - | 要比较的 bitset |
[edit] 返回值
1) 如果 *this 中每个位的值等于 rhs 中对应位的值,则返回 true,否则返回 false。
2) 如果 true 为 !(*this == rhs),则返回 true,否则返回 false。
[edit] 示例
比较给定的 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