C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ count()

C++ count()函数

C++ count()函数用于对数字的二进制表示形式中的设置位数进行计数。

语法

int count(); 

参数

不带任何参数。

返回值

它返回将位设置为整数值。

示例1

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b1(string("1100"));
int result=b1.count();
cout<<b1 << " has" <<' ' << result <<" bits";
return 0;
} 
输出:
1100 has 2 bits 

示例2

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<4> b1(16);
bitset<4> b2(18);
int result=b1.count();
int result1=b2.count();
cout<<b1 << " has" <<' ' << result <<" set bits" << '\n';
cout<<b2 << " has" <<' ' << result1 <<" set bits";
return 0;
} 
输出:
0000 has 0 set bits
0010 has 1 set bits 
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4