📜  C ++ STL中的位集test()

📅  最后修改于: 2021-06-01 01:56:22             🧑  作者: Mango

bitset :: test()是C++ STL中的内置函数,用于测试是否设置了给定索引处的位。

句法:

bitset_name.test(index) 

参数:该函数仅接受一个强制性参数索引,该索引指定是否设置该位的索引。

返回值:该函数返回一个布尔值。如果给定索引处的位设置,否则返回虚假它返回true。

下面的程序说明了上述函数:

程序1:

// CPP program to illustrate the
// bitset::test() function
// when bitset is a string
#include 
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("010010"));
  
    // Function to check if 2nd index bit
    // is set or not in b1
    if (b1.test(2))
        cout << "Bit at index 2 of 1100 is set\n";
    else
        cout << "Bit at index 2 is not set\n";
  
    // Function to check if 3nd index bit
    // is set or not in b2
    if (b2.test(3))
        cout << "Bit at index 3 of 010010 is set\n";
    else
        cout << "Bit at index 3 of 010010 is not set\n";
  
    return 0;
}
输出:
Bit at index 2 of 1100 is set
Bit at index 3 of 010010 is not set

程式2:

// CPP program to illustrate the
// bitset::test() function
// when the bitset is a number
#include 
using namespace std;
  
int main()
{
    // Initialization of bitset
    bitset<4> b1(5);
    bitset<6> b2(16);
  
    // Function to check if 2nd index bit
    // is set or not in b1
    if (b1.test(2))
        cout << "Bit at index 2 of 5 is set\n";
    else
        cout << "Bit at index 2 of 5 is not set\n";
  
    // Function to check if 3nd index bit
    // is set or not in b2
    if (b2.test(3))
        cout << "Bit at index 3 of 16 is set\n";
    else
        cout << "Bit at index 3 of 16 is not set\n";
  
    return 0;
}
输出:
Bit at index 2 of 5 is set
Bit at index 3 of 16 is not set
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”