比较 C++ 中的子位集
comparing sub-bitsets in C++
我有两个位集 std::bitset<200> A
和 std::bitset<200> B
,我想比较位 A[10-60]
和 B[50-100]
。我将两个位集中的 50 位提取到另外两个位集中,然后像下面这样比较它们。有没有更好的方法?
std::bitset<200> A, B;
// A and B are already set
std::bitset<50> x, y;
for(int i=10; i<=60; i++)
if(A.test(i)) x.set(i);
for(int i=50; i<=100; i++)
if(B.test(i)) y.set(i);
if( x == y) ....
将两个位集 A 和 B 一起循环怎么样?
bool same = true;
for (size_t ai = 10, bi = 50; ai != 60; ++ai, ++bi) {
if (A.test(ai) != B.test(bi) {
same = false;
break;
}
}
// same denotes if the sections of A and B are equal.
我有两个位集 std::bitset<200> A
和 std::bitset<200> B
,我想比较位 A[10-60]
和 B[50-100]
。我将两个位集中的 50 位提取到另外两个位集中,然后像下面这样比较它们。有没有更好的方法?
std::bitset<200> A, B;
// A and B are already set
std::bitset<50> x, y;
for(int i=10; i<=60; i++)
if(A.test(i)) x.set(i);
for(int i=50; i<=100; i++)
if(B.test(i)) y.set(i);
if( x == y) ....
将两个位集 A 和 B 一起循环怎么样?
bool same = true;
for (size_t ai = 10, bi = 50; ai != 60; ++ai, ++bi) {
if (A.test(ai) != B.test(bi) {
same = false;
break;
}
}
// same denotes if the sections of A and B are equal.