从std :: vector获取字节< bool> [英] Get bytes from a std::vector<bool>

查看:155
本文介绍了从std :: vector获取字节< bool>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似以下的东西,并在填充它任意数量的位后,我需要得到的字节写出到一个文件。我没有看到一个办法做到这一点,它似乎有用,所以我必须缺少的东西。任何想法?

I have something like the following, and after populating it with a arbitrary number of bits, I need to get the bytes to write out to a file. I don't see a way to do this and it seems useful, so I must be missing something. Any idea's?

std::vector<bool> a;

a.push_back(true);
a.push_back(false);
a.push_back(false);
a.push_back(true);

a.push_back(false);
a.push_back(true);
a.push_back(true);
a.push_back(false);


推荐答案

std :: vector< ; bool> 实际上不包含bools(iebytes),它包含位!这主要是一个缺点,建议您使用 std :: deque< bool> ,它没有这个功能。

std::vector <bool> does not actually contain bools (i.e.bytes) , it contains bits! This is mostly a missfeature and you are advised to use std::deque <bool>, which doesn't have this "feature" instead.

如果你想让存储是连续的,使用 std :: vector< char>

And if you want the storage to be contiguous, use std::vector <char>.

这篇关于从std :: vector获取字节&lt; bool&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆