在 C++ std::vector 和 C 数组之间转换而不复制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1733143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:02:31  来源:igfitidea点击:

Converting between C++ std::vector and C array without copying

c++carraysstlstdvector

提问by dzhelil

I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data.

我希望能够在 std::vector 与其底层 C 数组 int* 之间进行转换,而无需显式复制数据。

Does std::vector provide access to the underlying C array? I am looking for something like this

std::vector 是否提供对底层 C 数组的访问?我正在寻找这样的东西

vector<int> v (4,100)
int* pv = v.c_array();

EDIT:

编辑:

Also, is it possible to do the converse, i.e. how would I initialize an std::vectorfrom a C array without copying?

另外,是否可以反过来做,即如何在std::vector不复制的情况下从 C 数组初始化 an ?

int pv[4] = { 4, 4, 4, 4};
vector<int> v (pv);

回答by James McNellis

You can get a pointer to the first element as follows:

您可以获得指向第一个元素的指针,如下所示:

int* pv = &v[0];

This pointer is only valid as long as the vector is not reallocated. Reallocation happens automatically if you insert more elements than will fit in the vector's remaining capacity (that is, if v.size() + NumberOfNewElements > v.capacity(). You can use v.reserve(NewCapacity)to ensure the vector has a capacity of at least NewCapacity.

该指针仅在未重新分配向量时有效。如果您插入的元素多于矢量剩余容量(即,如果v.size() + NumberOfNewElements > v.capacity()。您可以使用v.reserve(NewCapacity)以确保矢量的容量至少为NewCapacity.

Also remember that when the vector gets destroyed, the underlying array gets deleted as well.

还要记住,当向量被破坏时,底层数组也会被删除。

回答by m.elahi

In c++11, you can use vector::data()to get C array pointer.

在 c++11 中,您可以使用vector::data()来获取 C 数组指针。

回答by Dаn

int* pv = &v[0]

Note that this is only the case for std::vector<>, you can not do the same with other standard containers.

请注意,这仅适用于std::vector<>,您不能对其他标准容器执行相同操作。

Scott Meyers covers this topic extensively in his books.

Scott Meyers 在他的书中广泛讨论了这个主题。

回答by Drew Hall

If you have very controlled conditions, you can just do:

如果您有非常可控的条件,您可以这样做:

std::vector<int> v(4,100);
int* pv = &v[0];

Be warned that this will only work as long as the vector doesn't have to grow, and the vector will still manage the lifetime of the underlying array (that is to say, don't delete pv). This is not an uncommon thing to do when calling underlying C APIs, but it's usually done with an unnamed temporary rather than by creating an explicit int* variable.

请注意,这仅在向量不必增长时才有效,并且向量仍将管理底层数组的生命周期(也就是说,不要删除 pv)。这在调用底层 C API 时并不罕见,但通常使用未命名的临时变量来完成,而不是通过创建显式 int* 变量来完成。

回答by user1270710

One way of protecting yourself against size changes is to reserve the maximal space (or larger) that you will need:

保护自己免受尺寸变化影响的一种方法是保留您需要的最大空间(或更大):

std::vector<int> v(4,100); //Maybe need 
v.reserve(40);             //reallocate to block out space for 40 elements

This will ensure that push_backs won't cause reallocation of the existing data.

这将确保 push_backs 不会导致现有数据的重新分配。