C++ c++;stl复制功能正确的内存管理

C++ c++;stl复制功能正确的内存管理,c++,memory-management,stl,deep-copy,C++,Memory Management,Stl,Deep Copy,就在下面的代码中,我需要用newforfloatarray分配内存,还是copy函数为我分配内存 也可以这样将常量向量复制到数组中吗?为什么? vector<float> floatVector; //filled floatVector here float *floatArray = new float[floatVector.size()]; copy(floatVector.begin(),floatVector.end(),floatArray); d

就在下面的代码中,我需要用newforfloatarray分配内存,还是copy函数为我分配内存

也可以这样将常量向量复制到数组中吗?为什么?

vector<float> floatVector;
//filled floatVector here

float *floatArray = new float[floatVector.size()];
copy(floatVector.begin(),floatVector.end(),floatArray);          
delete[] floatArray;
向量浮动向量;
//这里填充了向量
float*floatArray=newfloat[floatVector.size()];
复制(floatVector.begin()、floatVector.end()、floatArray);
删除数组;

std::copy
不分配内存,您应该自己做。

std::copy
不分配内存,您应该自己做。

copy不会为您分配,所以您需要分配。

copy不会为您分配,所以是的,您需要分配它。

std::使用arguments赋值(“=”)运算符或arguments copy构造函数复制副本(这可能取决于实现,我现在不确定)。它只在从param.begin()到param.end()的范围内进行迭代,并执行如下操作:

while (first!=last) *result++ = *first++;
因此,您需要自己分配必要的内存。否则将创建未定义的行为

这样将常量向量复制到数组中可以吗?取决于你想做什么。一般来说,这很好,因为值是通过值传递到目标的,而不是通过引用,因此常量的正确性不会被破坏

例如,这很好:

std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
const std::vector<int> constvec(vec);

int *arrray = new int[2];
std::copy(constvec.begin(),constvec.end(),arrray);
std::向量向量向量机;
向量推回(1);
向量推回(2);
常数std::向量常数vec(vec);
int*arrray=新int[2];
std::copy(constvec.begin(),constvec.end(),arrray);

std::使用arguments赋值(“=”)运算符或arguments copy构造函数复制副本(这可能取决于实现,我现在不确定)。它只在从param.begin()到param.end()的范围内进行迭代,并执行如下操作:

while (first!=last) *result++ = *first++;
因此,您需要自己分配必要的内存。否则将创建未定义的行为

这样将常量向量复制到数组中可以吗?取决于你想做什么。一般来说,这很好,因为值是通过值传递到目标的,而不是通过引用,因此常量的正确性不会被破坏

例如,这很好:

std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
const std::vector<int> constvec(vec);

int *arrray = new int[2];
std::copy(constvec.begin(),constvec.end(),arrray);
std::向量向量向量机;
向量推回(1);
向量推回(2);
常数std::向量常数vec(vec);
int*arrray=新int[2];
std::copy(constvec.begin(),constvec.end(),arrray);

如果您不想首先分配数组,那么可以使用
反向插入器
迭代器将元素逐个推入特定容器中。对于某些容器来说,这效率较低(我认为),但有时非常方便

#include<iterator>
#include<vector>
#include<deque>

std::vector<float> floatVector(10,1.0);
std::deque<float>  floatDeque; //memory not allocated

//insert elements of vector into deque.
std::copy(floatVector.begin(), floatVector.end(), std::back_inserter(floatDeque));
#包括
#包括
#包括
标准:向量浮动向量(10,1.0);
std:deque floatDeque//内存未分配
//将向量的元素插入到deque中。
std::copy(floatVector.begin()、floatVector.end()、std::back_插入器(floatDeque));

如果您不想首先分配数组,那么可以使用
反向插入器
迭代器将元素逐个推入特定容器中。对于某些容器来说,这效率较低(我认为),但有时非常方便

#include<iterator>
#include<vector>
#include<deque>

std::vector<float> floatVector(10,1.0);
std::deque<float>  floatDeque; //memory not allocated

//insert elements of vector into deque.
std::copy(floatVector.begin(), floatVector.end(), std::back_inserter(floatDeque));
#包括
#包括
#包括
标准:向量浮动向量(10,1.0);
std:deque floatDeque//内存未分配
//将向量的元素插入到deque中。
std::copy(floatVector.begin()、floatVector.end()、std::back_插入器(floatDeque));

复制函数从不分配内存,但您必须为静态数据结构分配内存,但对于动态数据结构,它将自动分配内存


这个过程不是很好,但最好使用另一个向量,而不是floatarray,copy函数从不分配内存,但必须为静态数据结构分配内存,但对于动态数据结构,它会自动分配内存


这个过程不是很好,但是最好使用另一个向量,而不是floatarray,你只是出于好奇问这个问题,不是吗?由于
&floatVector[0]
的行为与您的
floatArray
一样。确实可以这样做,但为什么?@honk。实际上,我在某个地方发现了内存泄漏,我想这可能是内存泄漏,但我猜错了:(我在项目中也没有执行删除操作,但将floatArray传递给了一个实例变量。如果您想查找内存泄漏(以及您在使用linux时的内存泄漏)在调试模式下签出valgrind。它会告诉你问题出在哪里。你只是出于好奇而问,不是吗?因为
&floatVector[0]
的行为就像你的
floatArray
一样。当然你可以这样做,但为什么?@honk。我实际上在某个地方发生了内存泄漏,我想可能是这样,但我猜错了:(我在项目中也没有执行删除操作,但将floatArray传递给了一个实例变量在调试模式下签出valgrind。它会给出问题所在的行。我以前从未见过back\u inserter,stl中隐藏了这么多好的实用程序!应该
back\u inserter
be
std::back\u inserter
?@Greg,是的,更正了!有很多可用的插入程序。如果你想要一个好的用法,请检查这个回答我的问题(我第一次了解他们的地方)从技术上讲,它将在没有显式的
std::
的情况下工作,因为
deque
位于
std
命名空间中,所以
back\u inserter
将通过ADL找到。也就是说,是的,您应该明确限定名称,以避免混淆和避免模糊的ADL问题。:-)我以前从未见过back\u inserter,stl中隐藏了这么多好的实用程序!应该
back\u inserter
be
std::back\u inserter
?@Greg,是的,更正了!有很多插件可用。如果你想要一个好的用法,请检查我问题的答案(我第一次了解它们的地方)从技术上讲,它将在没有显式
std::
的情况下工作,因为
deque
位于