The _Find_next() is a built-in function in C++ Bitset class which returns an integer which refers the position of next set bit in bitset after index. If there isn’t any set bit after index, _Find_next(index) will return the size of the bitset. Syntax:
iterator bitset._Find_next(index)
or
int bitset._Find_next(index)
Parameters: The function accepts one mandatory parameter index which specifies the index after which the first set bit is to be found in the bitset. Return Value: The function returns an integer which refers to the position of next set bit in bitset after specified index. If there isn’t any set bit after index(the specified index), _Find_next(index) will return the size of the bitset. Below is the illustration of the above function: Example:
CPP
#include <bits/stdc++.h>
using namespace std;
#define M 32
int main()
{
bitset<M> bset;
bitset<M> bset1;
bitset<M> bset2;
bset[5] = 1;
bset[10] = 1;
bset1[0] = bset1[12] = bset1[17] = bset1[30] = 1;
cout << "Next set bit after index 0 in bset\n";
cout << bset._Find_next(0) << "\n";
cout << "Next set bit after index 6 in bset\n";
cout << bset._Find_next(6) << "\n";
cout << "Find all set bits in bset1\n";
for ( int i = bset1._Find_first();
i < bset1.size();
i = bset1._Find_next(i))
cout << i << " ";
cout << "\n";
cout << "Next set bit after index 5 in bset2\n";
cout << bset2._Find_next(5) << "\n";
return 0;
}
|
Output:
Next set bit after index 0 in bset
5
Next set bit after index 6 in bset
10
Find all set bits in bset1
0 12 17 30
Next set bit after index 5 in bset2
32
Reference: https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/bitset-source.html
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Dec, 2022
Like Article
Save Article