How to make contiguous vector bools in C ++?

How to make an adjacent vector of values boolin C ++? I read a lot of warnings against usage std::vector<bool>, and I would like to keep the mask in std::vector<double>.

To make my task clearer, I would like to apply statistics for mine std::vector<double>for those elements in the vector boolthat are true. To do this at maximum performance, I would like the vector to be contiguous.

+4
source share
2 answers

You need to know what specific problems people find problematic std::vector<bool>since they are not necessarily applicable in your case. Probably the most important of them is that it does not necessarily touch. And here, the adjacent is probably wrong. The memory allocated for the whole vector will still be in pieces of contiguos, but &vec[0] + iwill not be the same as &vec[i].

However, many other qualities std::vectorare still preserved - it still provides random access at constant time, for example.

Bottom line - do not just be afraid, but understand the consequences.

+4
source
0

All Articles