當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Bitset all()用法及代碼示例

bitset all() 函數是 C++ STL(標準模板庫)的內置函數。此函數返回一個布爾值。如果調用位集的所有位都為 1,則返回值為真,否則將返回假。

該函數不接受任何參數並返回一個布爾值。

用法

Bool bitset_name .all()

樣本

Bitset = 100101

輸出

false

因為集合的所有位都需要為真才能返回真值。

示例

#include <bits/stdc++.h>
using namespace std;
void printer(bool val){
   if(val){
      cout<< "The bitset has all bits set"<< endl;
   } else{
      cout << "The bitset does not have all bits set"<< endl;
   }
}
int main() {
   bitset<4> bit1(string("1011"));
   bitset<6> bit2(string("111111"));
   cout<<"The bitset is "<<bit1<<endl;
   printer(bit1.all());
   cout<<"The bitset is "<<bit2<<endl;
   printer(bit2.all());
   return 0;
}

輸出

The bitset is 1011
The bitset does not have all bits set
The bitset is 111111
The bitset has all bits set

相關用法


注:本文由純淨天空篩選整理自Sudhir sharma大神的英文原創作品 Bitset all() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。