Variable size bitset [duplicate]

为君一笑 提交于 2019-11-30 01:51:41

问题


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 know of alternate approaches.


回答1:


The std::bitset<N> template requires a fixed size in advance. The std::vector<bool> is the C++ standard's way of providing a variable-length bitvector, and it offers functionality similar to a bitset that can grow and shrink.

As for whether it's better or worse to use vector<char> or vector<bool>: the vector<bool> is a much more direct way of accomplishing this goal. I would start off by using it, then switch to vector<char> if the performance is unacceptable. In general, it's good to try to write the cleanest, most straightforward implementation first, then to optimize later on.

Hope this helps!



来源:https://stackoverflow.com/questions/14433626/variable-size-bitset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!