Fra*_*ank 2 boost set boost-dynamic-bitset
我试图使用set的dynamic_bitset对象,但我在运行时得到一个断言失败:
a.out: boost/dynamic_bitset/dynamic_bitset.hpp:1291:
bool boost::operator<(const boost::dynamic_bitset<Block, Allocator>&,
const boost::dynamic_bitset<Block, Allocator>&)
[with Block = long unsigned int,
Allocator = std::allocator<long unsigned int>]:
Assertion `a.size() == b.size()' failed.
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <iostream>
#include <set>
#include <boost/dynamic_bitset.hpp>
int main() {
typedef boost::dynamic_bitset<> bitset;
std::set<bitset> myset;
bitset x(2, 0);
bitset y(3, 1);
myset.insert(x);
myset.insert(y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么插入的dynamic_bitset对象需要相同的大小。为了operator<起作用,是否不能假设较短位集中的最高有效位隐式地用零填充?
有什么办法可以使这套dynamic_bitsets起作用?
我也尝试过a,unordered_set因为它不需要the,operator<但是由于dynamic_bitset没有a hash_value而不能编译,而且我不确定如何在不使用其to_ulong成员函数的情况下编写该函数,该函数仅适用于短位集。
声明的原因operator<是实现的方式:
for (size_type ii = a.num_blocks(); ii > 0; --ii)
Run Code Online (Sandbox Code Playgroud)
仅第一个操作数的块计数用于遍历位集。如果第一个位集的大小较大,它将超出范围访问第二个位集。
您可以使用std :: set定义和使用自己的编译器,并根据需要处理不同大小的位集的比较:
struct my_less {
bool operator()(const boost::dynamic_bitset<>& lhs,
const boost::dynamic_bitset<>& rhs) const
{
//TODO: implement custom comparison for lhs < rhs
return false;
}
};
Run Code Online (Sandbox Code Playgroud)
typedef boost::dynamic_bitset<> bitset;
std::set<bitset,my_less> myset;
myset.insert( bitset(2, 0) );
myset.insert( bitset(3, 1) );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2211 次 |
| 最近记录: |