展开作为存储器传递的向量的大小 [英] expand size of vector passed as memory

查看:122
本文介绍了展开作为存储器传递的向量的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的向量传递给一个需要c数组的函数。它返回它填充的数据量(类似于fread)。有一种方法,我可以告诉我的向量来改变它的大小,以包括函数传递的量。

I am passing my vector to a function that expects a c array. It returns the amount of data it filled (similar to fread). Is there a way i can tell my vector to change its size to include the amount that function has passed in?

当然,我确保向量有capacity()

of course i make sure the vector has the capacity() to hold that amount of data.

推荐答案

不,没有支持的方法来展开一个向量,所以它包含额外的值

No, there is no supported way to "expand" a vector so it contains extra values that have been directly copied in. Relying on "capacity" to allocate non-sized memory that you can write to is definitely not something you should rely on.

您应该确保您可以使用您的向量具有所需的空间大小,然后调用函数,然后调整为正确的值后调整大小。例如

You should ensure your vector has the required amount of space by resizing before calling the function and then resizing to the correct value afterwards. E.g.

vector.resize(MAX_SIZE);
size_t items = write_array(&(vec[0]), MAX_SIZE)
vector.resize(items);

这篇关于展开作为存储器传递的向量的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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