正如(希望如此)我们都知道的那样,vector 完全被破坏,不能被视为 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 风格的数组.vector<bool> is specialized so that each bool takes 1 bit. Thus you can't convert it to a C-style array.当然,由于可能的对齐问题,我必须读入 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 容器.
Otherwise use an alternative vector that doesn't specialize on bool, such as the one in Boost Container.
这篇关于替代 vector<bool>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)