我需要 boost::dynamic_bitset<> 中第一次和最后一次出现 1 的索引,first 很容易找到,例如 size_type find_first() const。如何找到最后一个,我是否需要以相反的顺序迭代或创建新的,或者有更简单的方法可以找到技巧?
最佳答案
我们可以使用一些技巧,例如
#include <iostream>
#include <boost/dynamic_bitset.hpp>
int main()
{
typedef boost::dynamic_bitset<>::size_type size_type;
const size_type npos = boost::dynamic_bitset<>::npos;
boost::dynamic_bitset<> bitset(10, 50);
size_type first_idx = bitset.find_first();
size_type current_idx = first_idx;
if (first_idx != npos)
{
do {
current_idx = bitset.find_next(current_idx);
} while (bitset.find_next(current_idx) != boost::dynamic_bitset<>::npos);
std::cout << bitset << " first: " << first_idx << " last: " << current_idx << std::endl;
}
}
关于c++ - 如何在 boost::dynamic_bitset<> 中查找第一次和最后一次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13526368/