标准向量 C++――深拷贝或浅拷贝
我想知道是否复制向量我正在复制向量及其值(而这不适用于数组,并且深度复制需要循环或 memcpy).
I wonder whether copying a vector I am copying the vector with its values (whereas this is not working with array, and deep copy need a loop or memcpy).
你能提示解释吗?
问候
推荐答案
每次复制向量时,您都在进行深层复制.但是,如果您的向量是指针向量,则您将获得指针的副本,而不是指向的值
You are making a deep copy any time you copy a vector. But if your vector is a vector of pointers you are getting the copy of pointers, not the values are pointed to
例如:
std::vector<Foo> f;
std::vector<Foo> cp = f; //deep copy. All Foo copied
std::vector<Foo*> f;
std::vector<Foo*> cp = f; //deep copy (of pointers), or shallow copy (of objects).
//All pointers to Foo are copied, but not Foo themselves
相关文章