黑科技--位集--bitset
std::bitset
template <size_t N> class bitset;
Abtsetstores bits (elements with only two possible values: 0 or 1, true
or false
, ...). //位集用于存储0、1元素。
The class emulates an array of bool
elements, but optimized for space allocation: generally, each element occupies only one bit (which, on most systems, is eight times less than the smallest elemental type: char
). //这种类模拟了bool数组,但是单个元素占空间只有1bit。(!!好东西有木有!!)
Each bit position can be accessed individually: for example, for a given bitset named foo
, the expression foo[3]
accesses its fourth bit, just like a regular array accesses its elements. But because no elemental type is a single bit in most C++ environments, the individual elements are accessed as special references type (seebitset::reference). //每个位都能被独立访问。(!!好东西有木有!!)
Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and membersto_ulong andto_string ). They can also be directly inserted and extracted from streams in binary format (see applicable operators). //这货还提供转化成其他类型的函数(!!好东西有木有!!)
Thesize of a bitset is fixed at compile-time (determined by its template parameter). For a class that also optimizes for space allocation and allows for dynamic resizing, see the bool
specialization ofvector (vector<bool>
).
Template parameters
- N
- Size of the bitset, in terms of number of bits. It is returned by member functionbitset::size.size_t is an unsigned integral type. //大小由位数决定,并且可以引用内置函数直接查询某一bitset的大小
Member types
- reference
- Reference-like type (public member class )
Member functions
- (constructor)
- Construct bitset (public member function )
- applicable operators
- Bitset operators (function )
Bit access
- operator[]
- Access bit (public member function ) //访问位集中的元素可以直接像访问数组一样完成
- count
- Count bits set (public member function ) //计数有多少位
- size
- Return size (public member function ) //返回空间大小
- test
- Return bit value (public member function ) //返回...这什么东西?
- any
- Test if any bit is set (public member function ) //返回位集中是否有元素1
- none
- Test if no bit is set (public member function ) //返回位集是否全空
- all
- Test if all bits are set (public member function ) //返回位集是否全满
Bit operations
- set
- Set bits (public member function ) //设置位
- reset
- Reset bits (public member function ) //清空位集
- flip
- Flip bits (public member function ) //位集元素置反
Bitset operations
- to_string
- Convert to string (public member function )
- to_ulong
- Convert to unsigned long integer (public member function )
- to_ullong
- Convert to unsigned long long (public member function )
Non-member function overloads
- applicable operators
- Bitset operators (function )
Non-member class specializations
- hash<bitset>
- Hash for bitset (class template specialization )
【主要操作测试】
#include<iostream>
#include<cstdio>
#include<bitset> using namespace std; int main()
{
bitset<> a; a[]=; //直接赋值
cout << a.count() << endl; //计数1的个数
cout << a.size() << endl; //返回空间大小
cout << a.test() << endl;
cout << a.test() << endl; //判断第i位是否为1
cout << a.any() << endl; //是否非空
cout << a.none() << endl; //是否全空
cout << a.all() << endl; //是否全满 a.set(); //与赋值相同
cout << a[] << endl; //直接访问,与test相同 a.reset(); //清空
cout << a.count() << endl;
cout << a.none() << endl; a.flip(); //反置
cout << a.count() << endl;
cout << a.all() << endl;
}
【结果如下】
黑科技--位集--bitset的更多相关文章
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 微软AI发布会,集齐六大亮点召唤黑科技!
7月12日,微软合作伙伴大会Inspire在华盛顿特区如火如荼地举行.同一天,在相隔5个时区的伦敦,微软还举办了一场关于人工智能的发布会.这是一场智能技术和情感体验两相交融的科技盛宴,既有黑科技,也有 ...
- 【转载】史上最全:TensorFlow 好玩的技术、应用和你不知道的黑科技
[导读]TensorFlow 在 2015 年年底一出现就受到了极大的关注,经过一年多的发展,已经成为了在机器学习.深度学习项目中最受欢迎的框架之一.自发布以来,TensorFlow 不断在完善并增加 ...
- 一文带你了解 HTTP 黑科技
这是 HTTP 系列的第三篇文章,此篇文章为 HTTP 的进阶文章. 在前面两篇文章中我们讲述了 HTTP 的入门,HTTP 所有常用标头的概述,这篇文章我们来聊一下 HTTP 的一些 黑科技. HT ...
- 黑科技项目:英雄无敌III Mod <<Fallen Angel>>介绍
英雄无敌三简介(Heroes of Might and Magic III) 英3是1999年由New World Computing在Windows平台上开发的回合制策略魔幻游戏,其出版商是3DO. ...
- C++的黑科技
周二面了腾讯,之前只投了TST内推,貌似就是TST面试了 其中有一个问题,"如何产生一个不能被继承的类",这道题我反反复复只想到,将父类的构造函数私有,让子类不能调用,最后归结出一 ...
- 基于Twitter的Snowflake算法实现分布式高效有序ID生产黑科技(无懈可击)
参考美团文档:https://tech.meituan.com/2017/04/21/mt-leaf.html Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万 ...
- 这些JavaScript编程黑科技
1.单行写一个评级组件 "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate);定义一个变量rate是1到5的值,然后执行上面代码,看图 才发现插件什么的都 ...
- 优云亮相GOPS2017全球运维大会 “黑科技”获全场最高关注
2017年4月21日,GOPS――2017全球运维大会于深圳・圣淘沙酒店拉开帷幕.GOPS全球运维大会由高效运维社区(GreatOPS)和开放运维联盟(OOPSA)联合主办,由工信部信通院数据中心联盟 ...
随机推荐
- lvs + keepalived + httpd 高可用集群(转)
实验信息和拓扑:备注:Centos 6.5 selinux –disabled iptables off ServerName Ipaddress information LVSMaster 172. ...
- QML中MouseArea元素的介绍
原文:http://www.thisisqt.com/?action-viewnews-itemid-22 MouseArea元素的一个很典型的用法是和一个可视的item一起用,处理这个item的鼠标 ...
- 对象之function
开头啰嗦几句:程序之中,万事万物皆对象,这就是面向对象编程的核心思想,比如我们的js中的function,其实也是一个对象,是对象就可以有属性,在jq的源码中经常看到类似这样的方法: jQuery.E ...
- PHP中json序列化后中文的编码显示问题
在接口返回数据中,我们经常会返回json或者xml格式,php的json序列化函数json_encode非常好用,但是默认会把中文编码为ASCII码(注意,很多人认为这是乱码,其实不是),尤其在调试接 ...
- mysql具体语句示例
建表:(not null ,auto_increment, unique , primary key) create database balfish;use balfish;create table ...
- servlet多次跳转报IllegalStateException异常
当发生在如下错误的时候,有一个方案可行, "java.lang.IllegalStateException: Cannot forward after response has been c ...
- HTML day03表格与表单
1.表格 一般格式: <table> <thead><!--表格头--> <tr> <th></th> </tr>& ...
- c++模板两个数的加法
1.最简单的情况: template<class T> T Add(const T& a, const T& b) { return a + b; } 缺点是不能够处理不同 ...
- linux的学习系列 7---管道和过滤器
有时候,我们可以把两个命令连起来使用,一个命令的输出作为另一个命令的输入,这就叫做管道.为了建立管道,需要在两个命令之间使用竖线(|)连接. 管道是Linux进程之间一种重要的通信机制:除了管道,还有 ...
- div.2/Bellovin<最长上升子序列>
题意: 序列arr[i--n];输出以a[i]为结尾的最长上升子序列.1<=n<=100000; 思路: O(n*log(n)),求最长上升子序列. #include<cstdio& ...