首页 技术 正文
技术 2022年11月18日
0 收藏 430 点赞 3,121 浏览 984 个字

bitset中_Find_first()与_Find_next()函数

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

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

#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的位置

#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

#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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,027
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857