简体   繁体   English

从向量中提取 uint8_t* 子集<uint8_t></uint8_t>

[英]Extract uint8_t* subset from a vector<uint8_t>

I have a vector<uint8_t> , and I'm interfacing with an api that expects a uint8_t* data, size_t size .我有一个vector<uint8_t> ,我正在与一个 api 接口,它需要一个uint8_t* data, size_t size I need to provide the api with a subset of my vector, in my current solution I create a subset by using the vector constructor, and then I pass the data() of this new vector to the api:我需要为 api 提供我的向量的子集,在我当前的解决方案中,我使用向量构造函数创建一个子集,然后将这个新向量的 data() 传递给 api:

vector<uint8_t> subset(bytes.begin() + offset, bytes.begin() + offset + size);
api(subset.data(), subset.size());

This works fine in the majority of cases, but it is running out of memory on constrained devices.这在大多数情况下都可以正常工作,但在受限设备上 memory 已用完。 Is there a more efficient way to extract a uint8_t* subset from a vector<uint8_t> ?有没有更有效的方法从vector<uint8_t>中提取 uint8_t* 子集?

Yes, just combine the two but with data instead of begin .是的,只需将两者结合起来,但使用data而不是begin

api(bytes.data() + offset, size);

Using subscript and address-of operators also works使用下标和地址运算符也可以

api(&bytes[offset], size);

I Think you dont need to make a subset, pass vector's first element and size to want to take out of it.我认为您不需要创建一个子集,传递向量的第一个元素和大小即可从中取出。 Make sure this size is less than equal to bytes.size()确保此大小小于等于 bytes.size()

api(&bytes[0],size);

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

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