简体   繁体   English

我如何将布尔向量向量转换为char数组?

[英]how can I memcpy bool vector to char array?

I have two vectors. 我有两个向量。
One is 一个是

std::vector<unsigned char> one_v;

and the other is 另一个是

std::vector<bool> outputValuesInBits;

I pushed values to both one_v and outputValuesInBits. 我将值同时推送到one_v和outputValuesInBits。
Both vectors have two bytes. 两个向量都有两个字节。
one_v[0] and [1] are filled with data which would be 2 bytes. one_v [0]和[1]填充了2个字节的数据。
outputValuesInBits[0] to [15] are filled with data which would be 2 bytes. outputValuesInBits [0]至[15]填充了2个字节的数据。
Now, I would like to copy(memcpy) data to char array. 现在,我想将数据(memcpy)复制到char数组。

I can successfully copy data from one_v vector as following. 我可以成功地从one_v向量复制数据,如下所示。

 unsigned char* output = new unsigned char[one_v.size()]();
 memcpy(&output, one_v.data(), 2);

But I cannot copy data from outputValuesInBits. 但是我无法从outputValuesInBits复制数据。
If I do as follow, 如果我照做,

unsigned char* output = new unsigned char[outputValuesInBits.size()/8+1]();
memcpy(&output, outputValuesInBits.data(), 2);

It gives me an error 它给我一个错误

error: invalid use of void expression
     memcpy(&output, outputValuesInBits.data(), 2);

Can anyone please tell me how I can copy the bool vector to char array? 谁能告诉我如何将布尔向量复制到char数组?

Thank you in advance! 先感谢您!

std::vector<bool>没有数据成员函数

I'm afraid you cannot in a portable way. 恐怕您不能随身携带。 Cplusplus page on vector says: The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization. vector上的Cplusplus页上说: 该专业化与非专业化vector具有相同的成员函数,但本专业化中不存在的数据,emplace和emplace_back除外。 That means that data in not defined what explains the error when you try to use it. 这意味着在尝试使用data ,未定义的data会解释错误。

If portability is not an option, there will be no solution because The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit. 如果不选择可移植性,将没有解决方案,因为存储不一定是布尔值数组,但是库实现可以优化存储,以便将每个值存储在单个位中。 (emphasize mine). (强调我的)。 My understanding of the may is that you cannot even be sure that the 16 boolean are stored in 2 consecutive bytes: the implementation must only provide you a way to use them (almost) as if they were stored in 16 different booleans. 我对may的理解是,您甚至无法确定将16个布尔值存储在2个连续的字节中:该实现只能(几乎)为您提供一种使用它们的方式,就好像它们存储在16个不同的布尔值中一样。

If you can forget partability, you will have to find the source for you current implementation to know where and how the byte array is stored... but it is not that easy... 如果您忘记了可分区性,则必须找到当前实现的源,以了解字节数组的存储位置和存储方式……但这并不那么容易……

At least in g++ compiler you can use the _M_p member of the vector::iterator, which is the pointer to the data. 至少在g ++编译器中,可以使用vector :: iterator的_M_p成员,该成员是指向数据的指针。

Example: 例:

std::vector<bool> vBool(16, false);
vBool[0] = true;
vBool[2] = true;
vBool[13] = true;
std::vector<unsigned char> vChar(2);
unsigned short *ptrUS = reinterpret_cast<unsigned short *>( &(vChar[0]) );
*ptrUS = *reinterpret_cast<unsigned short *>(vBool.begin()._M_p);

std::cout << (unsigned int)vChar[0] << " " << (unsigned int)vChar[1] << "\n";

gives in output '5 32', which corresponds to the numbers with 1st and 3rd bit (5) and with the 6th bit (32). 给出输出“ 5 32”,它对应于具有第一和第三位(5)和第六位(32)的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM