当前位置:网站首页>【STL】位图的介绍使用以及代码的模拟实现
【STL】位图的介绍使用以及代码的模拟实现
2022-08-10 09:47:00 【flyyyya】
位图的引入
给40亿个不重复的无符号整数,没排过序。给一个无符号整数,如何快速判断一个数是否在这40亿个数中。【腾讯】
- 遍历,时间复杂度O(N)
- 排序(O(NlogN)),利用二分查找: logN
- 位图解决
数据是否在给定的整形数据中,结果是在或者不在,刚好是两种状态,那么可以使用一个二进制比特位来代表数据是否存在的信息,如果二进制比特位为1,代表存在,为0代表不存在。比如:
无符号整数总共有232个,因此记录这些数字就需要232个比特位,也就是512M的内存空间,内存消耗大大减少。
位图的概念
所谓位图,就是用每一位来存放某种状态,适用于海量数据,数据无重复的场景。通常是用来判断某个数据存不存在的。
位图的使用
位图的定义
1.构造一个16位的位图,所有位都初始化为0。
bitset<16> bs1; //0000000000000000
- 构造一个16位的位图,根据所给值初始化位图的前n位。
bitset<16> bs2(0xaa6);
3.构造一个16位的位图,根据字符串中的0/1序列初始化位图的前n位。
bitset<16> bs3(string("10111001")); //0000000010111001
成员函数的介绍
| 成员函数 | 功能 |
|---|---|
| set | 设置指定位为1 |
| reset | 清空指定位 |
| flip | 反转指定位 |
| test | 获取指定位的状态 |
| count | 获取被设置位的个数 |
| size | 获取可以容纳的位的个数 |
| any | 如果有任何一个位被设置则返回true |
| none | 如果没有位被设置则返回true |
| all | 如果所有位都被设置则返回true |
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<8> bs;
bs.set(2); //设置第2位
bs.set(4); //设置第4位
cout << bs << endl; //00010100
bs.flip(); //反转所有位
cout << bs << endl; //11101011
cout << bs.count() << endl; //6
cout << bs.test(1) << endl;
return 0;
}
注意: 使用成员函数set、reset、flip时,若指定了某一位则操作该位,若未指定位则操作所有位。
bitset运算符的使用。
一、对输入输出运算符的使用
bitset容器对>>、<<运算符进行了重载,可以直接对bitset定义出来的对象进行输入输出操作。
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
bitset<8> bs;
cin >> bs; //00110
cout << bs << endl; //00000110
return 0;
}
二、bitset中赋值运算符、关系运算符、复合赋值运算符、单目运算符的使用。
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main()
{
bitset<8> bs1(string("10101010"));
bitset<8> bs2(string("10101010"));
bs1 >>= 1;
cout << bs1 << endl; //01010101
//这里我们用的是按位或等运算符,有1位就变成1
bs2 |= bs1;
cout << bs2 << endl; //11111111
return 0;
}
三、bitset中位运算符的使用。
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main()
{
bitset<8> bs1(string("10101010"));
bitset<8> bs2(string("01010101"));
cout << (bs1&bs2) << endl; //00000000
cout << (bs1|bs2) << endl; //11111111
cout << (bs1^bs2) << endl; //11111111
return 0;
}
是异或的意思。他的规则是参加运算的两个二进位同号,则结果为0(假),异号则为1(真)即00=0,01=1,10=0,1^1=0(相同为0,相异为一);&是与运算,如果两个都是1,则结果是1,否则为0;|是或运算符,两个二进制数中只要有一个是1就为1,也就是除非两个数都是0,才为0。
边栏推荐
- Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
- BUUCTF【pwn】解题记录(4-6页持续更新中)
- 属性动画QPropertyAnimation
- 解决问题目录
- 【软考 系统架构设计师】系统可靠性分析与设计① 系统可靠性分析
- [System Design] S3 Object Storage
- 武功修炼:内功
- 俄罗斯宣布临时禁止进口摩尔多瓦植物产品
- 绘制温度曲线图;QChart,
- 「微服务架构」编曲与编舞——让系统协同工作的不同模式
猜你喜欢

初识Flink 完整使用 (第一章)

Basic concepts, structures, and classes of thread pools
![[Internet of Things Architecture] The most suitable open source database for the Internet of Things](/img/e9/10cf128dec3000daf7a3b2c816588f.jpg)
[Internet of Things Architecture] The most suitable open source database for the Internet of Things

12 【其它组合式API】

CatchAdmin实战教程(四)Table组件之自定义基础页面

郭晶晶家的象棋私教,好家伙是个机器人

【数据架构】分布式数据网格作为集中式数据单体的解决方案

【API 管理】什么是 API 管理,为什么它很重要?

以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会

用高质量图像标注数据加速AI商业化落地
随机推荐
并查集学习
关于编程本质那些事
04 【计算属性 侦听器】
Defending risks with technology and escorting cloud native | Tongchuang Yongyi X Boyun held a joint product launch conference
UE4 Sequence添加基础动画效果 (04-在序列中使用粒子效果)
dos环境下操作mysql
Static关键字及应用,继承的概念
「技术选型」工作流引擎哪家强?首席架构帮你挑
线程池的基本概念、结构、类
[Metaverse Omi Says] See how UCOUCO integrates performance art into the Metaverse
VBA: Inputbox Function and Inputbox Method
vs2012创建WCF应用程序
"Guangzhou highway engineering measures for the supervision and administration of production safety, and revised from six aspects
[System Design] S3 Object Storage
【数据仓库】什么是 Azure Synapse,它与 Azure Data Bricks 有何不同?
WebView2 通过 PuppeteerSharp 实现爬取 王者 壁纸 (案例版)
「业务架构」TOGAF建模:组织分解图(组织映射)
Development environment variable record under win
Oracle rac所在的网络要割接,停掉其中一个rac节点,这种方案可行吗?
武功修炼:招式