标准向量 C++——深拷贝或浅拷贝 [英] std vector C++ -- deep or shallow copy

查看:37
本文介绍了标准向量 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

这篇关于标准向量 C++——深拷贝或浅拷贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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