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


C++ bitset size()用法及代碼示例



bitset::size()是C++中的內置 STL,它返回位數。

用法:

bitset_name.size()

參數:該函數不接受任何參數。


返回值:該函數返回一個整數值,該整數值表示位數。最終,它返回初始化位集時已給出的大小。

以下示例程序旨在說明bitset::size()函數。

示例1:

// CPP program to illustrate the 
// bitset::size() function 
// when input is a string 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initilization of the bitset string 
    bitset<4> b1(string("1100")); 
    bitset<6> b2(string("010010")); 
  
    // prints the size i.e., 
    // the number of bits in "1100" 
    cout << "The number of bits in " << b1 
         << " is " << b1.size() << endl; 
  
    // prints the size i.e., 
    // the number of bits in  "010010" 
    cout << "The number of bits in " << b2 
         << " is " << b2.size() << endl; 
  
    return 0; 
}
輸出:
The number of bits in 1100 is 4
The number of bits in 010010 is 6

示例2:

// CPP program to illustrate the 
// bitset::flip() function 
// when input is a number 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initilization of the bitset string 
    bitset<3> b1(5); 
    bitset<5> b2(17); 
  
    // prints the size i.e., 
    // the number of bits in 5 
    cout << "The number of bits in " << b1 
         << " is " << b1.size() << endl; 
  
    // prints the size i.e., 
    // the number of bits in 17 
    cout << "The number of bits in "
         << b2 << " is " << b2.size() << endl; 
  
    return 0; 
}
輸出:
The number of bits in 101 is 3
The number of bits in 10001 is 5


相關用法


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