前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >bitset中_Find_first()与_Find_next()函数

bitset中_Find_first()与_Find_next()函数

作者头像
attack
发布2019-03-22 16:12:02
2.1K0
发布2019-03-22 16:12:02
举报

bitset中_Find_first()与_Find_next()函数

很有趣但是没怎么有用的两个函数。

_Find_fisrt就是找到从低位到高位第一个1的位置

代码语言:javascript
复制
#include<bits/stdc++.h>
int main() {
    std::bitset<1001> B;
    B.set(2); B.set(4); B.set(233);
    std::cout << B._Find_first();
}

输出结果为2

_Find_next就是找到当前位置的下一个1的位置

代码语言:javascript
复制
#include<bits/stdc++.h>
int main() {
    std::bitset<1001> B;
    B.set(2); B.set(4); B.set(233);
    std::cout << B._Find_next(5);
}

输出结果为233 1001,也就是说如果某个元素之后没有元素的话会返回bitset的大小

那么我们可以这样去遍历一个bitset

代码语言:javascript
复制
#include<bits/stdc++.h>
int main() {
    std::bitset<1001> B;
    B.set(2); B.set(4); B.set(233);
    for(int i = B._Find_first(); i != B.size(); i = B._Find_next(i)) 
        std::cout << i << ' ';
}

输出结果为2 4 233

按照糖教主的说法,这样遍历的复杂度是\(O(\frac{n}{w})\)的。\(n\)是bitset的大小,\(w\)与计算机有关,一般为\(32\)或\(64\)。也就是说遍历bitset的复杂度与bitset内1的个数无关

同时Swistakk大佬说

I don't remember it in details, but bitset in fact has a function for k-th bit, however it is declared as private... I have no idea why would someone not expose such useful function to world and deem it as private, but #define private public is there to help you

但是我翻了半天bitset的源代码也没找到与第K有关的函数qwq。如果有知道的大佬欢迎在评论区留言,本蒟蒻感激不尽

参考资料

bitset Find_first and Find_next

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • bitset中_Find_first()与_Find_next()函数
  • 参考资料
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档