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 :) 9 9887
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?
> 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.
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?
> 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.
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.
Uzytkownik "Gunnar G" <de****@comhem. se> napisal w wiadomosci
news:oX******** ************@ne wsc.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
> 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;
}
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(co py_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_pt r<Foo> > vec_of_base_poi nters;
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<Fo o> > shared_vec;
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
std::vector<int> fun(){
//build the vector v;
return v;
}
|
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:
vector<string> tokenize(string s){
|
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 non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).
Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither...
|
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 format, 3 pp)
My question, coming from a C++ background where deep copying is done,
is why in C# you would do either deep or shallow copying as suggested
by O'Reilly (using the "ICloneable" inhereited interface), at least
for the .NET framework.
...
|
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 follow:
I have a smart pointer my_ptr, and the copy constructor uses deep
copy, however I want it to be shallow copy in std::vector when
reallocate memory. How can I implement it?
| |
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 base poiner can have multipull meanings.
i know i can use dynamic_cast however i really don't like this
solution and i was wondering is i can do something better
|
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 snippet below. I have some objects which are
derived (subclassed?, subtyped?) from simpler ones, in increasing
size. There is a linear hierarchy. I need to keep them all in some
sort of linked list (perhaps std::vector). I could have several
|
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 has string value. I
really do not understand why push_back() function is trying to remove
previously inserted data.
Thanks for any help
|
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 representation of vector<string>? Will the vector
contain the string objects or will it contain pointers/references to the
string objects? The reason I ask is that it is not clear to me how the
v.reserve() and &v operations would work for a vector<string>.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |