📜  在C++ STL中对any()进行位设置

📅  最后修改于: 2021-05-30 09:57:36             🧑  作者: Mango

bitset :: any()是C++ STL中的内置函数,如果数字中至少设置了一位,则返回True。如果未设置所有位或数字为零,则返回False。

句法:

bool any() 

参数:该函数不接受任何参数。

返回值:该函数返回一个布尔值。如果设置了布尔值的任何一位,则返回的布尔值为True 。如果未设置任何位,则为False

下面的程序说明了bitset :: any()函数。

程序1:

C++
// C++ program to illustrate the
// bitset::any() function
#include 
using namespace std;
 
int main()
{
    // initialization
    bitset<4> b1(string("1100"));
    bitset<6> b2(string("000000"));
 
    // function to check if any of
    // its bits are set or not
    bool result1 = b1.any();
    if (result1)
        cout << b1 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b1 << " does not have any bits set"
             << endl;
 
    // function to check if any of
    // its bits are set or not
    bool result2 = b2.any();
    if (result2)
        cout << b2 << "  has a minimum of one-bit set"
             << endl;
    else
        cout << b2 << " does not have any bits set"
             << endl;
 
    return 0;
}


C++
// C++ program to illustrate the
// bitset::any() function
// when the input is as a number
 
#include 
using namespace std;
 
int main()
{
    // initialization
    bitset<2> b1(3);
    bitset<3> b2(0);
 
    // function to check if any of
    // its bits are set or not
    bool result1 = b1.any();
    if (result1)
        cout << b1 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b1 << " does not have any bit set"
             << endl;
 
    // function to check if any of
    // its bits are set or not
    bool result2 = b2.any();
    if (result2)
        cout << b2 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b2 << " does not have any bit set"
             << endl;
 
    return 0;
}


输出:
1100 has a minimum of one-bit set
000000 does not have any bits set




程式2:

C++

// C++ program to illustrate the
// bitset::any() function
// when the input is as a number
 
#include 
using namespace std;
 
int main()
{
    // initialization
    bitset<2> b1(3);
    bitset<3> b2(0);
 
    // function to check if any of
    // its bits are set or not
    bool result1 = b1.any();
    if (result1)
        cout << b1 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b1 << " does not have any bit set"
             << endl;
 
    // function to check if any of
    // its bits are set or not
    bool result2 = b2.any();
    if (result2)
        cout << b2 << " has a minimum of one-bit set"
             << endl;
    else
        cout << b2 << " does not have any bit set"
             << endl;
 
    return 0;
}
输出:
11 has a minimum of one-bit set
000 does not have any bit set




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