C++容器 bitset
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<10>first;//empty bitsetcout<<first<<endl;//输出10个0bitset<10>second(120ul);//initlize from unsigned long//初始化为无符号型整数120bitset<10>a(5);cout<<a<<endl;//5的二进制数printf("%d\n",a);//输出5bitset<32>b(-5);cout<<b<<endl;//-5的二进制数(补码表示)printf("%d\n",b);//输出-5bitset<10>c(-5);cout<<c<<endl;//-5的二进制数(补码表示)printf("%d\n",c);//输出10位二进制数转化为十进制的结果bitset<10>d(string("000101"));//initalize from stringcout<<d<<endl;printf("%d\n",d);//将01字符串转化为十进制正整数bitset<10>e(string("1111111011"));//initalize from stringcout<<e<<endl;printf("%d\n",e);//将01字符串转化为十进制正整数return 0;
}
相关函数:
1.any():
#include<iostream>
#include<bitset>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<32>mybits;cin>>mybits;if(mybits.any())//任何一位被设置就返回truecout<<"mybits has "<<(int)mybits.count()<<"bits set\n";elsecout<<"No bit set\n";return 0;
}
2.none():
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<32>mybits;cin>>mybits;if(mybits.none())//没有任何一位被设置就返回truecout<<"No bit set\n";elsecout<<"mybits has "<<(int)mybits.count()<<"bits set\n";return 0;
}
3.count():
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<8> myset(string("10110011"));cout<<int(myset.count())<<" ones\n";cout<<int(myset.size()-myset.count())<<" zeros\n";return 0;
}
4.set():
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4> mybits;cout<<mybits.set()<<endl;//1111cout<<mybits.set(2,0)<<endl;//1011cout<<mybits.set(2)<<endl;//1111return 0;
}
5.test():
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);string bitval("01011");bitset<5> mybits(bitval);cout<<boolalpha;for(size_t i=0;i<mybits.size();++i)cout<<mybits.test(i)<<endl;//返回指定位的状态return 0;
}
6.reset():
我们就可以做到这一点.例如:
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4> mybits(string("1011"));cout<<mybits.reset(2)<<endl;//1001cout<<mybits.reset()<<endl;//0000return 0;
}
7.flip():
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4> mybits(string("0001"));cout<<mybits.flip(2)<<endl;//0101cout<<mybits[2].flip()<<endl;//0cout<<mybits.flip()<<endl;//1010return 0;
}
8.to_ulong();
返回bitset的整数表示
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);//string bitval("01011");string bitval="01011";bitset<5> mybits(bitval);cout<<mybits.to_ulong()<<endl;//返回bitset的整数表示return 0;
}
9.to_string():
返回bitset的字符串表示
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<5> mybits;//0000mybits.set();//1111string s=mybits.to_string();cout<<s<<endl;//返回bitset的字符串表示return 0;
}
10.operator[]()
返回第x位的引用
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<5> mybits;//0000mybits[1]=1;mybits[2]=mybits[1];cout<<mybits<<endl;return 0;
}
11.operatorss()
比较和赋值运算
#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;int main()
{ios::sync_with_stdio(0);bitset<4>first(string("1001"));bitset<4>second(string("0011"));cout<<(first^=second)<<endl;//1010cout<<(first&=second)<<endl;//0010cout<<(first|=second)<<endl;//0011cout<<endl;cout<<(first<<=2)<<endl;//1100cout<<(first>>=1)<<endl;//0110cout<<endl;cout<<(~second)<<endl;//1100cout<<(second<<1)<<endl;//0110cout<<(second>>1)<<endl;//0001cout<<endl;cout<<(first==second)<<endl;//falsecout<<(first!=second)<<endl;//truecout<<endl;cout<<(first&second)<<endl;//0010cout<<(first|second)<<endl;//0111cout<<(first^second)<<endl;//0101cout<<endl;return 0;
}
C++容器 bitset相关推荐
- 标准非STL容器 : bitset
1. 概念 什么是"标准非STL容器"?标准非STL容器是指"可以认为它们是容器,但是他们并不满足STL容器的所有要求".前文提到的容器适配器stack.que ...
- c++高级编程学习笔记5
multimap multimap 是一种允许多个元素使用同一个键的 map.和 map 一样,multimap 支持统一初始化.multimap的接口和 map 的接口几乎相同,区别在于multim ...
- C++ STL常用库的使用方法(下)
文章目录 (一)deque 双端队列容器 1) 创建 deque 对象 2) 插入元素 3) 遍历 4)删除元素 (二) list 双向链表容器 1) 创建 list 对象 2) 元素插入和遍历 3) ...
- 你不知道的C++ STL库
STL 提供三种类型的组件:容器.迭代器和算法,它们都支持泛型程序设计标准. 容器主要有两类:顺序容器和关联容器. 顺序容器(vector. list. deque 和 string等)是一系列元素的 ...
- STL之multiset中equal_range,multimap中的equal_range,bitset容器,string字符串操作,lambda表达式
1multiset中用equal_range来遍历所有的元素 #include <set> #include <iostream> using namespace std ...
- C++语言基础 —— STL —— 容器与迭代器 —— bitset
[概述] bitset 位于 <bitset> 头文件中,其是 STL 的一部分,准确地说,bitset 是一个模板类而不是容器,其模板参数不是类型,而整型的数值,即:bitset < ...
- C++ 容器及选用总结
原文地址 目录 ==================================================== 第一章 容器 第二章 Vector和string 第三章 关联容器 第四章 迭 ...
- 【C++标准库】特殊容器
特殊容器,又称为容器适配器(Container Adapter),它们改造了标准STL容器,使之满足特殊的要求. Stack堆栈 使用stack时,需包含头文件<stack> push() ...
- 容器删除元素后迭代器失效_STL 4: STL之容器:选择时机,删除元素,迭代器失效...
一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist和rope.sl ...
最新文章
- 【原创】如何分析一个网站使用的服务器类型
- 汇编 头文件 依赖库文件
- 什么是透传通道?(透明传输通道,就是当中继使)
- php switch case 判断语句,PHP的switch判断语句的“高级”用法详解,switch详解_PHP教程...
- C# 多态相关的文章
- request重定向_golang不想http自动处理重定向的解决方案
- java httpclient4_httpclient4使用说明
- Spring自学日志04(java的方式配置Spring)
- 雷军发“玄妙”知识微博:暗示小米MIX4 将采用42W快充?
- android-清单小文件
- C++ 标准库 —— random
- SpringBoot项目引入前端模板时,CSS、JS、HTML文件无法加载的问题
- python的cubes怎么使用_Python之Cubes框架使用
- CentOS8安装MySQL可视化客户端
- JavaSE知识框架
- 走进“开源SDR实验室” 一起玩转4G/5G开源项目srsRAN(srsLTE升级版):安装方法+NB-IoT测试
- 项目1 :家庭记账系统
- 【数据分析】 Titanic乘客获救预测(2)数据处理
- Android Build 获取手机信息
- SQL语句常见面试题(一)
热门文章
- Nginx服务器的Web请求处理机制
- MVC与单元测试实践之健身网站(八)-统计分析
- C. Anton and Making Potions 贪心 + 二分
- WinServer-AD操作常用powershell命令
- 用于正则表达式的 Regex.Matches静态方法的几种用法
- 计算机组成原理实验箱使能开关,计算机组成原理微程序计数器实验报告
- c语言千位数字,C语言怎样提取一个数的十位个位百位千位?
- android webview 重定向 多次load问题,解决webview 第二次调用loadUrl页面不刷新的问题...
- python的模块提供了许多文件管理方法_学会这几招,轻松掌握Python文件管理
- jquery ajax html方法吗,jQuery ajax方法