c++ - 如何在 boost::dynamic_bitset<> 中查找第一次和最后一次出现

标签 c++ boost

我需要 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/

相关文章:

c++ - 字段 'buffer_' 的类型不完整 'boost::array<char , 4096ul>'

c++ - 如何使用 boost :gil:png_write_view on x64 避免此警告

c++ - Linux: C++:/usr/bin/ld: 找不到 -llibboost_serialization

c++ - 读取训练数据失败 : tagger. cpp (393) CRF++

c++ - 如何从写入文件的 UTF-8 编码 URDU 字符串中获取单个字符?

c++ - 如何为延迟评估的类方法提供 STL 容器?

c++ - 为什么在按值返回时创建临时对象而不是在按值传递给函数参数时创建临时对象

c++ - 带有线程包装器 unique_ptr 的双端队列

c++ - 通用线程池类无法正常工作

c++ - C++ 中的 shared_ptr 和引用