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. 电商、商城类APP常用标签"hot"--第三方开源--LabelView

    LabelView是在github上一个开源的标签库.其项目主页是:https://github.com/linger1216//labelview LabelView为一个TextView,Imag ...

  2. leetCode刷题记录

    (1)Linked List Cycle Total Accepted: 13297 Total Submissions: 38411 Given a linked list, determine i ...

  3. Spark官方文档——独立集群模式(Standalone Mode)

    除了部署在Mesos之上, Spark也支持独立部署模式,包括一个Spark master进程和多个 Spark worker进程.独立部署模式可以运行在单机上作为测试之用,也可以部署在集群上.如果你 ...

  4. 清空FORM表单的几种方式 Reset 重加载

    1. form中定义name <form name = "sbform" action="sb_add.php" method="post&qu ...

  5. Jquery权威指南

    1.Radio <input id="Radio1" name="rdoSex" type="radio" value="男 ...

  6. OXPattern

    10000的随机数组由ox组成,查找数组中oox...x(任意x)oox....x(任意x)o的个数 enum { DATA_SIZE = , }; enum enum_status { STATUS ...

  7. STM32F0xx_ADC采集电压配置详细过程

    前言 关于ADC这一块的功能基本上也算是CortexM芯片的标配了.ST的每一块芯片都有这个功能,只是说因型号不同,通道数.位数等有所不同.STM8的芯片大多数都是10的,也就是说分辨率可达到:参考电 ...

  8. WPF DragDrop事件元素跟随

    前一段时间项目里面要实现一个鼠标拖动一个元素到另外一个元素上面并且赋值的功能,由于要在surface上运行,拖动的时候手指会挡住系统默认的拖动图标,导致用户意识不到自己是不是在拖动着东西,所以要解决这 ...

  9. DataSet中的relation

    DataSet中的relation DataSet是ADO.Net中相当重要的数据访问模型.有一个很大的优点是可以记录多个表之间的关系.有点类似与数据库的外键. 在DataSet中也可以定义类似的关系 ...

  10. 用Python作GIS之五:从示例入手—example函数

    进入STARS后,最简单的学习方法就是演示示例数据.对于源码的分析也可以从这里入手.        以下为出发菜单项“Example Project”的函数example:def example(se ...