Alternative to vectorlt;boolgt;(替代向量lt;boolgt;)
问题描述
(希望)我们都知道,vector<bool> 已完全损坏,不能被视为 C 数组.获得此功能的最佳方法是什么?到目前为止,我想到的想法是:
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:
- 使用
vector代替,或 - 使用包装类并具有
vector
你们如何处理这个问题?我需要 c_array() 功能.
How do you guys handle this problem? I need the c_array() functionality.
作为一个附带问题,如果我不需要 c_array() 方法,如果我需要随机访问,解决此问题的最佳方法是什么?我应该使用双端队列还是其他什么?
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?
- 我确实需要动态调整大小.
- 对于那些不知道的人,
vector是专门的,因此每个bool占用 1 位.因此,您不能将其转换为 C 样式的数组. - 我猜包装"有点用词不当.我在想这样的事情:
- I do need dynamic sizing.
- For those who don't know,
vector<bool>is specialized so that eachbooltakes 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:
当然,由于可能的对齐问题,我必须读入 my_bool :(
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;
推荐答案
使用 std::deque 如果你不需要数组,是的.
Use std::deque if you don't need the array, yes.
否则,请使用不专门用于 bool 的替代 vector,例如 Boost Container.
Otherwise use an alternative vector that doesn't specialize on bool, such as the one in Boost Container.
这篇关于替代向量<bool>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:替代向量<bool>
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
