问题描述
我正在练习一个数组中的问题,我必须在其中找到独特的元素。现在为此,我的逻辑是找到数组中的最大元素,并为其定义bitset。但是问题是bitset需要一个恒定的值,所以如何克服这个,下面是我的一些问题:
a)我可以无限次地定义bitset可变大小?
b)如果没有,那么使用 vector< bool>
或 vector的最佳方法是什么? char>
?
c)我知道boost有一个动态的位,但是我正在做这个学习,我想知道其他方法。
std :: bitset< N>
模板需要提前一个固定的大小。 std :: vector< bool>
是C ++标准提供可变长度位向量的方式,它提供类似于可以增长和收缩的位元的功能。对于使用 vector< char>
或向量< bool>是否更好或更坏,p>
:矢量< bool>
是一种更直接的方式来实现这一目标。如果性能不可接受,我将开始使用它,然后切换到 vector< char>
。一般来说,最好先写最干净,最直接的实现,然后再进行优化。
希望这有帮助!
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.
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!
这篇关于可变大小的位组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!