当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。