vector assignment operations
-
Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)
-
Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)
misha_grewal wrote:
What happens to the objects contained in pVect when the assignment operator is called?
There aren't any objects in pVect when the assignment operator is called. The best code to use would probably be
pVect = new std::vector<myclass>(vect.begin(), vect.end());
This allocates the new vector and copies the contents of vect in one operation.
-
misha_grewal wrote:
What happens to the objects contained in pVect when the assignment operator is called?
There aren't any objects in pVect when the assignment operator is called. The best code to use would probably be
pVect = new std::vector<myclass>(vect.begin(), vect.end());
This allocates the new vector and copies the contents of vect in one operation.
Thanks a lot Stuart, I have one more doubt..
Stuart Dootson wrote:
What happens to the objects contained in pVect when the assignment operator is called? There aren't any objects in pVect when the assignment operator is called.
But what if pVect already has some objects for e.g. *pVect = vect; //(this time vect has 10 objects) //Again at some point in code *pVect = vect; //(this time vect has 5 objects) Regards, Misha
-
Thanks a lot Stuart, I have one more doubt..
Stuart Dootson wrote:
What happens to the objects contained in pVect when the assignment operator is called? There aren't any objects in pVect when the assignment operator is called.
But what if pVect already has some objects for e.g. *pVect = vect; //(this time vect has 10 objects) //Again at some point in code *pVect = vect; //(this time vect has 5 objects) Regards, Misha
The objects will be destructed, so for
std::vector<myclass> a;
the
myclass
destructor will be called for each member ofa
if you assign something else toa
-
Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)
misha_grewal wrote:
std::vector *pVect = new std::vector;
STL containers are generally designed with stack semantics in mind. Of course, it is allowed to create them on the heap, but it is rarely a good thing.
-
Hi all - I am little confused over the assignment mechanism in std::Vector. I have a standard vector of some class object, lets say MyClass. This vector has various number of objects at various occasions. I also need to keep a backup of this vector, at times. I am using following code: 1. std::vector vect; 2. std::vector *pVect = new std::vector; //(one time initialization in the owner class's ctor) // for assignment 3. *pVect = vect; What happens to the objects contained in pVect when the assignment operator is called? Is it better to use a resize here? Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)
I am confused! :confused: Q1) Why are you using
new
at all? :doh:misha_grewal wrote:
3. *pVect = vect;
The objects in
pVect
are destroyed and then replaced by copies of those stored invect
; the vector will resize its self if needed.misha_grewal wrote:
Or is it better if I use this code -- // for assignment if (pVect) delete pVect; pVect = new std::Vector(vect)
In this scenario you are wasting time and code: 1) You do not need to use
if(pVect)
in C++, becausedelete
does that for you. So you would just calldelete
. 2) See Q1 above. Note: Usingnew
is rarely required in a well written (standard) C++ program.INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra