替代矢量&lt;&布尔GT; [英] Alternative to vector<bool>
问题描述
由于(希望)大家都知道,矢量&lt;&布尔GT;
完全破碎,不能作为C数组处理。是什么让这个功能的最佳方式?
到目前为止,我想到的思路是:
- 使用一个
矢量&lt;&字符GT;
来代替,或 - 使用一个包装类,并有
矢量&lt;&bool_wrapper GT;
你们如何处理这个问题?我需要c_array()功能。
作为一个方面的问题,如果我不需要c_array()方法是什么,如果我需要随机存取解决这个问题的最好方法?我应该使用双端还是其他什么东西?
编辑:
- 我需要动态调整大小。
- 对于那些不知道是谁,
矢量&lt;&布尔GT;
专业,使每个布尔需要1位。因此,你不能将它转换为C数组。 - 我猜包装是一个有点用词不当。我想是这样的:
当然,那么我要读成my_bool由于可能对齐的问题:(
结构my_bool
{
布尔the_bool;
};
矢量&lt; my_bool&GT; haha_i_tricked_you;
使用 的std :: deque的
如果不需要数组,是的。
否则使用替代矢量
不专注于布尔
,比如一个在<一个href=\"http://www.boost.org/doc/libs/1_48_0/doc/html/container/Cpp11_conformance.html#container.Cpp11_conformance.Vector_bool\">Boost集装箱。
As (hopefully) we all know, vector<bool>
is totally broken and can't be treated as a c array. What is the best way to get this functionality?
So far, the ideas I have thought of are:
- Use a
vector<char>
instead, or - Use a wrapper class and have
vector<bool_wrapper>
How do you guys handle this problem? I need the c_array() functionality.
As a side question, if I don't need the c_array() method, what is the best way to approach this problem if I need random access? Should I use a deque or something else?
Edit:
- I do need dynamic sizing.
- For those who don't know,
vector<bool>
is specialized so that each bool takes 1 bit. Thus you can't convert it to a c-style array. - I guess "wrapper" is a bit of a misnomer. I was thinking something like this:
Of course, then I have to read into a my_bool due to possible alignment issues :(
struct my_bool
{
bool the_bool;
};
vector<my_bool> haha_i_tricked_you;
Use std::deque
if you don't need the array, yes.
Otherwise use an alternative vector
that doesn't specialize on bool
, such as the one in Boost Container.
这篇关于替代矢量&lt;&布尔GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!