473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

vector with deep copy

Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)
Jul 22 '05 #1
9 9840
Gunnar G wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)


That depends how deep you want the copy to be... If a vector contains
pointers, do you want the new copy to contain new pointers to copies of
the originally referenced elements? Can you guarantee that none of the
original pointers was null, and that all of them pointed to valid
objects? If the original pointers were of type pointer-to-A, but some
of the objects actually were of some subclass of A, how do you avoid
slicing?
Jul 22 '05 #2
> That depends how deep you want the copy to be... If a vector contains
pointers, do you want the new copy to contain new pointers to copies of
the originally referenced elements? That's whats meant by deep copy, isn't it?
Anyway, that is what I want.
Can you guarantee that none of the original pointers was null,
and that all of them pointed to valid objects? Null pointers should never be stored in the container, so yes, that is
guaranteed.
If the original pointers were of type pointer-to-A, but some
of the objects actually were of some subclass of A, how do you avoid
slicing?

Well, I have to read more about slicing, but I would certainly want to have
an object of the subclass in the copy if that is what was in the original.
Can't I assume that the user is using virtual functions in their classes,
and then everything is perfectly happy?

I've read something about a virtual constructors in Bjarne's book, guess
I'll have to read more.

Jul 22 '05 #3
On Mon, 10 May 2004 11:05:29 GMT, Gunnar G <de****@comhem.se> wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?


std::vector does perform deep copies - it copies contained elements
using their copy constructors.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
Gunnar G wrote:
That depends how deep you want the copy to be... If a vector contains
pointers, do you want the new copy to contain new pointers to copies of
the originally referenced elements?
That's whats meant by deep copy, isn't it?


Not necessarily. The elements "contained" by a std::vector aren't
actually members of the container class, they're held in memory pointed
to by a member variable. Since the referenced memory is copied, a
vector already is doing a deep copy.
Anyway, that is what I want.

Can you guarantee that none of the original pointers was null,
and that all of them pointed to valid objects?
Null pointers should never be stored in the container, so yes, that is
guaranteed.


You mean under your particular circumstances, or are you asserting that
null pointers should never be stored in std::vector?
If the original pointers were of type pointer-to-A, but some
of the objects actually were of some subclass of A, how do you avoid
slicing?


Well, I have to read more about slicing, but I would certainly want to have
an object of the subclass in the copy if that is what was in the original.
Can't I assume that the user is using virtual functions in their classes,
and then everything is perfectly happy?


No, everything is not then perfectly happy.
I've read something about a virtual constructors in Bjarne's book, guess
I'll have to read more.


At this point, you're already imposing some pretty tough rules on the
types of element that can be stored in the vector. It sounds like you
need an "intrusive" container, very different from std::vector. Why
don't you implement your own, possibly using std::vector as part of the
implementation, and post your code here for feedback?
Jul 22 '05 #5
> std::vector does perform deep copies - it copies contained elements
using their copy constructors.

and if I copy a

vector<Foo*> x ?

Will it not only copy the addresses of the Foo elements? That is AFAIK a
shallow copy.
Jul 22 '05 #6
In article <Zl********************@newsb.telia.net>,
Gunnar G <de****@comhem.se> wrote:
Is there anything like vector in STL, that performes deep copy of the
elements it contains?

I hope this will appear in future releases of STL :)


vector already does this. Simply hold a vector of objects that perform
deep copy when copied and vector will perform a deep copy of each object.
Jul 22 '05 #7

Uzytkownik "Gunnar G" <de****@comhem.se> napisal w wiadomosci
news:oX********************@newsc.telia.net...
std::vector does perform deep copies - it copies contained elements
using their copy constructors.

and if I copy a

vector<Foo*> x ?

Will it not only copy the addresses of the Foo elements? That is AFAIK a
shallow copy.


Yes it will copy only addresses. But that is what it contains - so this is
actually a 'deep copy'. std::vector does not dereference the stored pointers
in any way and treats them exactly as if they were, say, ints.

If you want std::vector to copy objects, create a vector with objects, not
with pointers to them:

std::vector<Foo> x;

regards,
Marcin
Jul 22 '05 #8
> At this point, you're already imposing some pretty tough rules on the
types of element that can be stored in the vector. It sounds like you
need an "intrusive" container, very different from std::vector. Why
don't you implement your own, possibly using std::vector as part of the
implementation, and post your code here for feedback?


I guess we have been talking about different things, Yes I'm gonna write my
own container, I just asked if someone had done this before me so I didn't
have to reinvent the wheel :-)

Anyway, std::vector does not make a deep copy (as I define deep copy), at
least not with gcc-3.0. If it did a deep copy, it would give different
adresses below.

#include <iostream>
#include <vector>

using namespace std;

class Foo{
public:
int x;
Foo(){x=1;}
Foo(int q):x(q){}
};

int main(){
vector<Foo*> a,b;
Foo* t=new Foo(3);
a.push_back(t);
cout<<"Adress "<<a[0]<<endl;
b=a;
cout<<"Adress "<<b[0]<<endl;
}
Jul 22 '05 #9
In article <Ds********************@newsc.telia.net>,
Gunnar G <de****@comhem.se> wrote:
At this point, you're already imposing some pretty tough rules on the
types of element that can be stored in the vector. It sounds like you
need an "intrusive" container, very different from std::vector. Why
don't you implement your own, possibly using std::vector as part of the
implementation, and post your code here for feedback?


I guess we have been talking about different things, Yes I'm gonna write my
own container, I just asked if someone had done this before me so I didn't
have to reinvent the wheel :-)

Anyway, std::vector does not make a deep copy (as I define deep copy), at
least not with gcc-3.0. If it did a deep copy, it would give different
adresses below.

#include <iostream>
#include <vector>

using namespace std;

class Foo{
public:
int x;
Foo(){x=1;}
Foo(int q):x(q){}
};

int main(){
vector<Foo*> a,b;
Foo* t=new Foo(3);
a.push_back(t);
cout<<"Adress "<<a[0]<<endl;
b=a;
cout<<"Adress "<<b[0]<<endl;
}


Another approach you might consider besides implementing a container is
implementing a smart pointer: copy_ptr<T>:

copy_ptr<Foo> a(new Foo(3));
copy_ptr<Foo> b(a); // makes copy with new Foo(const Foo&)
assert(a != b);

Then you can just:

vector<copy_ptr<Foo> > va;
va.push_back(copy_ptr<Foo>(new Foo));

An advantage to this approach is that if you decide you need different
semantics on copy, it is easier to change to a new smart pointer rather
than rewrite your container. For example if Foo is a base class with a
virtual clone function (typical virtual copy ctor) you could create a
clone_ptr:

clone_ptr<Foo> a(new DerivedFromFoo(3));
clone_ptr<Foo> b(a); // makes copy with a->clone()
assert(a != b);
....
vector<clone_ptr<Foo> > vec_of_base_pointers;

A high quality refcounted pointer is available at boost (www.boost.org).
The same design is also in the first library technical report and thus
may be available with your C++ compiler under namespace std::tr1 (e.g.
Metrowerks Pro 9). So you could:

vector<std::tr1::shared_ptr<Foo> > shared_vec;

-Howard
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
82
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create...
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
2
by: Tim | last post by:
Dear All, std::vector seems using the copy constructor to allocate memory when needed. Is it possible to control the memory allocation, such as by using my own allocator? My question is as...
10
by: Alex Snast | last post by:
hello I'm trying to implement a copy constructor for a vector of pointers to a base class (which is abstract) My question is how would i know what kind of new type should i allocate since the...
3
by: Ramon F Herrera | last post by:
Newbie alert: I come from C programming, so I still have that frame of mind, but I am trying to "Think in C++". In C this problem would be solved using unions. Hello: Please consider the...
10
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.