頗有趣可是沒怎麼有用的兩個函數。c++
_Find_fisrt
就是找到從低位到高位第一個1的位置ide
#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的位置idea
#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的大小spa
那麼咱們能夠這樣去遍歷一個bitsetcode
#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
。blog
按照糖教主的說法,這樣遍歷的複雜度是\(O(\frac{n}{w})\)的。\(n\)是bitset的大小,\(w\)與計算機有關,通常爲\(32\)或\(64\)。也就是說遍歷bitset的複雜度與bitset內1的個數無關rem
同時Swistakk大佬說get
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 youit
可是我翻了半天bitset的源代碼也沒找到與第K有關的函數qwq。若是有知道的大佬歡迎在評論區留言,本蒟蒻感激涕零