bitset

C++ enum flags vs bitset

倖福魔咒の 提交于 2019-12-01 02:39:02
What are pros/cons of usage bitsets over enum flags? namespace Flag { enum State { Read = 1 << 0, Write = 1 << 1, Binary = 1 << 2, }; } namespace Plain { enum State { Read, Write, Binary, Count }; } int main() { { unsigned int state = Flag::Read | Flag::Binary; std::cout << state << std::endl; state |= Flag::Write; state &= ~(Flag::Read | Flag::Binary); std::cout << state << std::endl; } { std::bitset<Plain::Count> state; state.set(Plain::Read); state.set(Plain::Binary); std::cout << state.to_ulong() << std::endl; state.flip(); std::cout << state.to_ulong() << std::endl; } return 0; } As I can

C++STL手写版

▼魔方 西西 提交于 2019-11-30 23:02:31
手写STL,卡常专用。 node为变量类型,可以自由定义,以下不再赘述。 1、stack(栈)   开一个数组,和一个top指针,压栈时++,弹栈时--即可。 struct stack{ int tp;node st[N]; node top(){return st[tp];} void push(node x){st[++tp]=x;} void pop(){--tp;} bool empty(){return !tp;} void size(){return tp;} void clear(){tp=0;} }s; 2、queue(循环队列)   用两个指针,记录队头和队尾,达到两端时特判即可。 struct queue{ int l,r,cnt;node qu[N]; node front(){return qu[l];} void ne(int &x){++x;if(x==N) x=1;} void push(node x){ne(r);qu[r]=x;++cnt;} void pop(){ne(l);--cnt;} bool empty(){return !cnt;} void size(){return cnt;} void clear(){r=cnt=0,l=1;} }q; 3、deque(双端队列)   和队列类似。 struct deque{ int l,r

STLのbitset

别等时光非礼了梦想. 提交于 2019-11-30 18:55:05
bitset——高端压位卡常题必备STL bitset存储二进制数位。 bitset就像一个bool类型的数组一样,但是有空间优化——bitset中的一个元素一般只占1 bit,相当于一个char元素所占空间的八分之一。 bitset中的每个元素都能单独被访问,例如对于一个叫做foo的bitset,表达式foo[3]访问了它的第4个元素,就像数组一样。 bitset有一个特性:整数类型和布尔数组都能转化成bitset。 bitset的大小在编译时就需要确定。如果你想要不确定长度的bitset,请使用(奇葩的)vector<bool>。 bitset的运算就像一个普通的整数一样,可以进行 与(&) 或(|) 异或(^) 左移(<<) 右移(>>)等操作 bitset的相关函数 对于一个叫做foo的bitset: foo.size() 返回大小(位数) foo.count() 返回1的个数 foo.any() 返回是否有1 foo.none() 返回是否没有1 foo.set() 全都变成1 foo.set(p) 将第p + 1位变成1 foo.set(p, x) 将第p + 1位变成x foo.reset() 全都变成0 foo.reset(p) 将第p + 1位变成0 foo.flip() 全都取反 foo.flip(p) 将第p + 1位取反 foo.to_ulong()

Why are the bits of a std::bitset in reverse order? [duplicate]

我与影子孤独终老i 提交于 2019-11-30 18:26:10
This question already has an answer here: Why does std::bitset expose bits in little-endian fashion? 2 answers Why does bitset store the bits in reverse order? After strugging many times I have finally written this binary_to_dec. Could it simplified? int binary_to_dec(std::string bin) { std::bitset<8> bit; int c = bin.size(); for (size_t i = 0; i < bin.size(); i++,c--) { bit.set(c-1, (bin[i]-'0' ? true : false)); } return bit.to_ulong(); } Bitset stores its numbers in what you consider to be "reverse" order because we write the digits of a number in decreasing order of significance even though

Variable size bitset [duplicate]

末鹿安然 提交于 2019-11-30 18:07:16
This question already has an answer here: Define bitset size at initialization? 6 answers I am practicing a question on array in which I have to find unique elements. Now for this my logic is to find the max element in the array and define the bitset for that. But problem is bitset needs a constant value so how to overcome this, below are some of my question on this: a) Can I, by any chance, define the bitset with a variable size? b) If not, then what is the best approach to use vector<bool> or vector<char> ? c) I know boost has a dynamic bitset but as I am doing this for learning I want to

关于bitset

假装没事ソ 提交于 2019-11-30 16:51:15
当我们遇到这样一种情况: 输入n串二进制数 然后对这些二进制数进行一些位运算 再以二进制输出 一般我们会想到用字符串输入这些二进制数然后转存进int或者long long进行位运算 最后再写一个输出二进制的函数用来输出 乍一看,难度不大,码量也还行 但是也没有一种数据结构能够直接存储二进制数呢 答案肯定是有的 不然我BB半天是为了什么 bitset是C++自带的用来储存二进制数的一种数据结构(在bitset头文件中) 它的好处是能够直接用来读入二进制数和进行位运算 但是不能读二进制以上的数 并且int类型和bool类型可以转换为bitset(long long之类的整型也可以) 但是字符数组依然不行 bitset还有一个好处就是空间小,一个元素只占1bit,只有char的1/8大 bitset中的每一个元素都可以被单独访问,但是不能被单独读入或读出 bitset还自带了一些函数(如果有遗漏请指出): bitset <10> s; s.any(); //返回在bitset中是否存在1(即bitset存储的二进制数是否不为0) s.set(); //将bitset所有的元素全部变为1 s.set(p); //将bitset的第p+1个元素变为1 s.set(p, x); //将bitset的第p+1个元素变为x(x为1或0) s.flip(); //将bitset中所有的元素全部取反

Why are the bits of a std::bitset in reverse order? [duplicate]

风流意气都作罢 提交于 2019-11-30 16:47:10
问题 This question already has answers here : Why does std::bitset expose bits in little-endian fashion? (2 answers) Closed 3 years ago . Why does bitset store the bits in reverse order? After strugging many times I have finally written this binary_to_dec. Could it simplified? int binary_to_dec(std::string bin) { std::bitset<8> bit; int c = bin.size(); for (size_t i = 0; i < bin.size(); i++,c--) { bit.set(c-1, (bin[i]-'0' ? true : false)); } return bit.to_ulong(); } 回答1: Bitset stores its numbers

How to write bitset data to a file? [duplicate]

早过忘川 提交于 2019-11-30 08:50:04
问题 This question already has answers here : How does one store a vector<bool> or a bitset into a file, but bit-wise? (6 answers) Closed 3 years ago . I have a std::bitset that I'd like to write to a file, bit for bit, but of course fstream's write function doesn't support this. I can't think of another way besides converting each 8-bit group to a char using string and writing that... Anyone know of a good way? 回答1: Try: #include <bitset> #include <fstream> int main() { using namespace std; const

Shifting a Java BitSet

我怕爱的太早我们不能终老 提交于 2019-11-30 08:23:07
I am using a java.util.BitSet to store a dense vector of bits. I want to implement an operation that shifts the bits right by 1, analogous to >>> on ints. Is there a library function that shifts BitSet s? If not, is there a better way than the below? public static void logicalRightShift(BitSet bs) { for (int i = 0; (i = bs.nextSetBit(i)) >= 0;) { // i is the first bit in a run of set bits. // Set any bit to the left of the run. if (i != 0) { bs.set(i - 1); } // Now i is the index of the bit after the end of the run. i = bs.nextClearBit(i); // nextClearBit never returns -1. // Clear the last

分组背包+二分图染色 (bitset+滚动数组优化)

六眼飞鱼酱① 提交于 2019-11-30 03:03:25
题目链接 题意:给你n种物品,有m种限制条件,然后输入n种物品的价值,m种限制条件,表示第i个物品不能与第j个物品分成一组,求把物品分成两组后,价值差最小,输出其中最大的价值。 思路:对于每一个限制条件,都可以看成一棵树,我们对这棵树染色,分成两组,然后把每一组都取上,输出答案。 #include<bits/stdc++.h> using namespace std; const int maxn=1e5+100; vector<int>G[205]; int a[205],c[205],val[3]; bitset<maxn> d[2]; void dfs(int u) //染色 { val[c[u]]+=a[u]; for(auto v : G[u]) { if(!c[v]) { c[v]=3-c[u]; dfs(v); } } } int main() { int T; cin>>T; while(T--) { d[0].reset(); //把 bitset 置0; int n,m,sum=0; cin>>n>>m; memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { cin>>a[i]; G[i].clear(); a[i]/=100; //题目数据说是整百的 sum+=a[i]; } for(int i=1;i<=m;i++)