1
我試圖使用dynamic_bitset
的set
一個對象,但我在運行時得到一個斷言失敗:如何使用一組boost :: dynamic_bitsets?
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.
下面是代碼:
#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;
}
我想知道爲什麼插入的dynamic_bitset
對象的大小必須相同。爲了使operator<
正常工作,難道它不能假定較短比特中的最高有效位被隱式填充爲零嗎?
有什麼辦法可以讓dynamic_bitset
的工作集合起來嗎?
我也嘗試了unordered_set
,因爲它不需要operator<
但它不能編譯,因爲dynamic_bitset
沒有hash_value
,我不知道該怎麼寫,而不使用其to_ulong
成員函數,這隻適用於短的位集。