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。如果有知道的大佬歡迎在評論區留言,本蒟蒻感激不盡