template <size_t N> class bitset;

Bitset
A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).
[bitset存储位(元素只能为两种可能的数值,即0或1,true或false,...)]
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).
[bitset类似于存储bool型元素的数组,但会优化空间配置。一般来说,bitset中的每一个元素都只占一位(在大多数系统下,bitset中每一个元素所占位数都是最小数据类型char所占位数的1/8)]
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 (see bitset::reference).
[bitset中的每一位都可以被单独读取,举个例子,有一个名为foo的bitset,则表达式foo[3]就像一个普通数组获取其元素那样,表示获取foo的第4位]
Bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see its constructor and members to_ulong and to_string). They can also be directly inserted and extracted from streams in binary format (see applicable operators).
[bitset可以通过整数值或者二进制字符串构造,也可以转换为整数值或者二进制字符串(参见bitset的构造函数及其成员函数to_ulong和to_string)。bitset也可以直接插入或者读取自二进制格式的流中(参见applicable operators)]
The size 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 of vector (vector<bool>).
[bitset的长度是在编译期间确定的(由它的模板参数决定),如果想要使用一个可以在运行期间再确定长度且也能够优化空间配置的类,请参见专门为bool设计的类vecotr<bool>]

/*
bitset(); bitset (unsigned long val); template<class charT, class traits, class Alloc>
explicit bitset (const basic_string<charT,traits,Alloc>& str, typename basic_string<charT,traits,Alloc>::size_type pos = 0, typename basic_string<charT,traits,Alloc>::size_type n = basic_string<charT,traits,Alloc>::npos); Construct bitset
Constructs a bitset container object:
[构造一个bitset容器对象]
(1) default constructor
The object is initialized with zeros.
[默认构造函数:对象初始化为0]
(2) initialization from integer value
Initializes the object with the bit values of val:
[用整数值初始化]
(3) initialization from string
Uses the sequence of zeros and/or ones in str to initialize the first n bit positions of the constructed bitset object.
[用str中前n个0或1来构造bitset对象] Note that bitset objects have a fixed size (determined by their class template argument) no matter the constructor used: Those bit positions not explicitly set by the constructor are initialized with a value of zero.
[需要注意的是,bitset对象的长度在编译时就已经确定,如果构造bitset时没有显示地指定相应bit的值,则对应bit初始化为0] val
Integral value whose bits are copied to the bitset positions.
- If the value representation of val is greater than the bitset size, only the least significant bits of val are taken into consideration.
[如果val的长度比bitset的长度大,则只考虑val的最低有效位(即顶端被截除)]
- If the value representation of val is less than the bitset size, the remaining bit positions are initialized to zero.
[如果val的长度比bitset的长度小,则剩余的位被初始化为0] str
A basic_string whose contents are used to initialize the bitset:
The constructor parses the string reading at most n characters beginning at pos, interpreting the character values '0' and '1' as zero and one, respectively.
[构造函数读取string中从pos开始的n个(至多n个)字符用来初始化bitset]
If this sequence is shorter than the bitset size, the remaining bit positions are initialized to zero.
[如果string的长度比bitset的长度小,则剩余的位数被初始化为0]
*/ #include <iostream>
#include <string>
#include <bitset> int main()
{
std::bitset<> foo;
std::bitset<> bar(0xfa2);
std::bitset<> baz(std::string("")); std::cout<<"foo: "<<foo<<'\n'; //
std::cout<<"bar: "<<bar<<'\n'; //10100010, the least significant bits of val are taken into consideration
std::cout<<"baz: "<<baz<<'\n'; //01011110, reading at most n characters beginning at pos system("pause");
return ;
}
/*
std::bitset operators // member functions
bitset& operator&= (const bitset& rhs);
bitset& operator|= (const bitset& rhs);
bitset& operator^= (const bitset& rhs);
bitset& operator<<= (size_t pos);
bitset& operator>>= (size_t pos);
bool operator== (const bitset& rhs) const;
bool operator!= (const bitset& rhs) const;
bitset operator~() const;
bitset operator<<(size_t pos) const;
bitset operator>>(size_t pos) const; // non-member functions
template<size_t N>
bitset<N> operator& (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
bitset<N> operator| (const bitset<N>& lhs, const bitset<N>& rhs);
template<size_t N>
bitset<N> operator^ (const bitset<N>& lhs, const bitset<N>& rhs); // iostream inserters/extracto
template<class charT, class traits, size_t N>
basic_istream<charT, traits>& operator>> (basic_istream<charT,traits>& is, bitset<N>& rhs);
template<class charT, class traits, size_t N>
basic_ostream<charT, traits>& operator<< (basic_ostream<charT,traits>& os, const bitset<N>& rhs); Bitset operators
Performs the proper bitwise operation using the contents of the bitset.
[按位操作bitset]
*/ #include <iostream>
#include <string>
#include <bitset> int main()
{
std::bitset<> foo(std::string(""));
std::bitset<> bar(std::string("")); std::cout<<(foo^=bar)<<'\n'; //1010 (XOR, assign)
std::cout<<(foo&=bar)<<'\n'; //0010 (AND, assign)
std::cout<<(foo|=bar)<<'\n'; //0011 (OR, assign) std::cout<<(foo<<=)<<'\n'; //1100 (SHL, assign)
std::cout<<(foo>>=)<<'\n'; //0110 (SHR, assign) std::cout<<(~bar)<<'\n'; //1100 (NOT)
std::cout<<(bar<<)<<'\n'; //0110 (SHL)
std::cout<<(bar>>)<<'\n'; //0001 (SHR) std::cout<<std::boolalpha<<(foo==bar)<<'\n'; //false (0110==0011)
std::cout<<(foo!=bar)<<'\n'; //true (0110!=0011) std::cout<<(foo&bar)<<'\n'; //
std::cout<<(foo^bar)<<'\n'; //
std::cout<<(foo|bar)<<'\n'; // system("pause");
return ;
}
/*
bool none() const; //检查二进制位是否全部为0,是则返回真
bool any() const; //检查二进制位是否有1,有则返回真
size_t count() const; //计算bitset中为1的位的个数
size_t size() const; //返回bitset的位数
bool test(size_t pos) const; //返回bitset中pos位上的值
reference operator[] (size_t pos) //返回bitset中pos位上的值的引用 bitset& flip();
bitset& flip(size_t pos); //反置bitset中所有的位,如果指定pos,那么只有pos上的位被反置 bitset& reset()
bitset& reset(size_t pos); //重置二进制位(全部设置为0),如果指定pos,那么只有pos上的位被重置 bitset& set();
bitset& set(size_t pos, bool val = true); //设置二进制位(全部设置为1),如果指定pos,那么只有pos上的位被设置为v to_string(); //返回bitset的string形式
to_ulong(); //返回bitset的unsigned long形式
*/ #include <iostream>
#include <string>
#include <bitset> int main()
{
std::bitset<> mybits;
mybits.set(); std::string mystring = mybits.to_string();
std::cout<<"mystring: "<<mystring<<'\n'; // unsigned long myulong = mybits.to_ulong();
std::cout<<"myulong: "<<myulong<<'\n'; // system("pause");
return ;
}

标准非STL之bitset的更多相关文章

  1. 标准非STL容器 : bitset

    1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...

  2. 玩家属性同步优化-脏数据标记(位运算、数组、stl之bitset)

    把大神的帖子中一部分摘抄出来,结合自己写的位运算代码和循环代码(数组遍历)进行性能测试分析并给出结果. 摘自: https://www.gameres.com/827195.html 本文适用于所有脏 ...

  3. 队列问题非STL解决方案

    队列问题非STL解决方案 常年使用STL解决队列问题,以至于严重生疏队列的根本原理... 直到今日 被老师被迫 使用算法原理解决问题,方才意识到我对队列一窍不通... ...直到 经过一系列的坑蒙拐骗 ...

  4. [技术] OIer的C++标准库 : STL入门

    注: 本文主要摘取STL在OI中的常用技巧应用, 所以可能会重点说明容器部分和算法部分, 且不会讨论所有支持的函数/操作并主要讨论 C++11 前支持的特性. 如果需要详细完整的介绍请自行查阅标准文档 ...

  5. STL容器 -- Bitset

    核心内容:Bitset 是 STL 中的二进制容器, 存放的时 bit 位元素, 每一位只占一个 bit 位, 取值 0 或者 1, 可以像整形元素一样按位与或非, 并且大大优化了时间和空间复杂度. ...

  6. STL中bitset的用法

    终于又来写博客了 == bitset存储的是二进数位,就和一个bool性数组差不多.用法上和数组的操作方式也差不多. 每位只占一个字节,大大优化了空间,可以通过数组形式访问. bitset定义 可以用 ...

  7. HDU 4280Island Transport(Dinc非STL 模板)

    题意: n岛m条路,然后是 n个岛的坐标,然后是m条双向路,包括 岛和 岛 之间 最大客流量,让求 最左边的岛 到右边的岛 最大客流量 分析: 建图 以 左边的岛为原点,最右边的为终点求最大客流量. ...

  8. calc 多项式计算 (STL版和非STL版) -SilverN

    计算(calc.cpp) [问题描述] 小明在你的帮助下,破密了Ferrari设的密码门,正要往前走,突然又出现了一个密码门,门上有一个算式,其中只有“(”,“)”,“0-9”,“+”,“-”,“*” ...

  9. 位运算 进制转化 STL中bitset用法

    2017-08-17 16:27:29 writer:pprp /* 题目名称:输入十进制以二进制显示 程序说明:同上 作者:pprp 备注:无 日期:2017/8/17 */ #include &l ...

随机推荐

  1. Unieap3.5Java端常用公用方法

    String OrgId = McssComMethod.getDimensionID(); Date systemDate =  DataStoreUtil.getOracleSystemDate( ...

  2. Unieap3.5-Grid编辑列中数字与下拉改变

    Grid列表中字段改变事件 <cell label="单据金额" width='20%' name="NFEE_1" id="NFEE_1&qu ...

  3. Android IOS WebRTC 音视频开发总结(五四)-- WebRTC标准之父谈WebRTC

    本文主要是整理自国内首届WebRTC大会上对Daniel的一些专访,转载必须说明出处,欢迎关注微信公众号blacker,更多说明详见www.rtc.help 说明:以下内容主要整理自InfoQ的专访, ...

  4. IIS报错 未将对象引用设置到对象的实例。

    在vs中运行正常的项目 ,发布到IIS总是提示 未将对象引用设置到对象的实例. 运行静态页面 html正常,只是打开.aspx页面的时候报错,在确保了数据库,配置,权限均正常的情况下. 错误原因:先安 ...

  5. div模拟下拉框

    1.模拟下拉框.点击文本框在文本框下面显示一个层divList,点击divList以外的任何地方,关闭divList层 document.body.onclick = function (e) { e ...

  6. 为什么swing不适合做桌面软件

    http://www.zhihu.com/question/19608871 我最近几年做的项目清一色的都是HTML5了,这篇<基于HTML5的电信网管3D机房监控应用>供参考,HTML5 ...

  7. bootstrap中弹出窗体dialog的自定义

    感谢nakupanda的https://github.com/nakupanda/bootstrap3-dialog 根据需要弹出窗体,但是可以移动,不遮挡下面的内容,所以就修改了源代码,添加了一个属 ...

  8. C++判断对称三位数素数

    题目内容:判断一个数是否为对称三位数素数.所谓“对称”是指一个数,倒过来还是该数.例如,375不是对称数,因为倒过来变成了573. 输入描述:输入数据含有不多于50个的正整数(0<n<23 ...

  9. 关于VS2012下安装破解文件Visual Assit X的一点说明

    今天在使用Visual Studio 2012的时候,编写代码的助手Visual Assit X突然提示我说,试用期已过,要求我输入一个注册码,我靠,这货不是几个月前已经破解了吗,怎么今天傻不愣登的提 ...

  10. 蒙牛乳业六厂—第一家MES工厂

    在上海西门子工业自动化(SIAS)与蒙牛液态奶事业部以及蒙牛集团信息中心的共同努力下,经过项目组成员1年半时间的具体实施,中国乳品行业第一个真正意义上的生产执行系统MES,于2008年6月在蒙牛乳业集 ...