C++ STL 中的 bitset all() 函式


bitset all() 函式是 C++ STL(標準模板庫) 的一個內建函式。此函式會返回一個布林值。如果呼叫 bitset 的所有位都是 1,則會返回 true,否則將返回 false。

此函式不接受任何引數,並返回一個布林值。

語法

Bool bitset_name .all()

示例

Bitset = 100101

輸出

false

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

示例

#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

更新於: 09-08-2019

117 次瀏覽

開啟您的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.