C++ Bitset 中的 any()函数

首页 / C++入门教程 / C++ Bitset 中的 any()函数

C++ bitset any()函数用于测试是否至少设置了bitset中的一位。它返回一个布尔值,即true或false。

any - 语法

bool any();

any - 返回值

它返回布尔值0或1。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-bitset-any-function.html

来源:LearnFk无涯教程网

any - 例子1

#include <iostream>
#include <bitset>
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;
}

输出:

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

any - 例子2

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b1(string("1100"));
bitset<4> b2(string("00"));
bool result1 = b1.any();
if (result1)
cout<< b1.count() << " has a minimum of one-bit set"
<<endl;
else
cout<< b1.count() << " does not have any bits set"
<<endl;
bool result2 = b2.any();
if (result2)
cout<< b2.count() << " has a minimum of one-bit set"
<<endl;
else
cout<< b2.count() << " does not have any bits set"
<<endl;
return 0;
}

输出:

2 has a minimum of one-bit set
0 does not have any bits set

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员进阶攻略 -〔胡峰〕

Linux实战技能100讲 -〔尹会生〕

数据分析思维课 -〔郭炜〕

零基础实战机器学习 -〔黄佳〕

PyTorch深度学习实战 -〔方远〕

深入浅出可观测性 -〔翁一磊〕

超级访谈:对话毕玄 -〔毕玄〕

零基础GPT应用入门课 -〔林健(键盘)〕

Midjourney入门实践课 -〔Jovi〕

好记忆不如烂笔头。留下您的足迹吧 :)