📜  带有示例的C++位集中的_Find_next()函数

📅  最后修改于: 2021-05-30 06:55:05             🧑  作者: Mango

_Find_next()是C++ Biteset类中的内置函数,该函数返回一个整数,该整数引用索引后位集中下一个设置位的位置。如果index之后没有任何设置位,则_Find_next(index)将返回该位集的大小。

句法:

iterator bitset._Find_next(index)
           or
int bitset._Find_next(index)

参数:该函数接受一个强制性参数索引,该索引指定在位集中找到第一个置位位之后的索引。

返回值:该函数返回一个整数,该整数表示指定索引后位集中下一个设置位的位置。如果index(指定的index)之后没有任何设置位,则_Find_next(index)将返回该位集的大小。

下面是上述函数的说明:

例子:

// C++ program for illustration
// of _Find_next() function
  
#include 
using namespace std;
  
#define M 32
  
int main()
{
    // default constructor initializes with all bits 0
    bitset bset;
    bitset bset1;
    bitset bset2;
  
    // 00000000000000000000000000100000
    bset[5] = 1;
  
    // 00000000000000000000010000100000
    bset[10] = 1;
  
    // 01000000000000100001000000000001
    bset1[0] = bset1[12] = bset1[17] = bset1[30] = 1;
  
    // function returns the next set bit
    // in bitset after index 0
    cout << "Next set bit after index 0 in bset\n";
    cout << bset._Find_next(0) << "\n";
  
    // function returns the next set bit
    // in bitset after index 6
    cout << "Next set bit after index 6 in bset\n";
    cout << bset._Find_next(6) << "\n";
  
    // finds all set bits in bitset bset1
    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";
  
    // function returns bset2.size()
    // when there isn't any set bit after index
    cout << "Next set bit after index 5 in bset2\n";
    cout << bset2._Find_next(5) << "\n";
  
    return 0;
}
输出:
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

参考: https : //gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.4/bitset-source.html

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”