TheSnowman
asked on
Inserting into a vector
What is the correct way to insert a new object into a vector? For example:
// Create vector
vector<CMyClass> vObjects;
// Add a whole bunch of stuff
........
// Now insert something in the middle
vObjects.insert (&vObjects[3], new CMyClass);
That last line causes an error. It can't convert the pointer to a reference. So I try adding a & or * to it along with a mixture of parenthesis in the vain hope that something will click, but nothing. How do I get a reference out of that pointer? I know I ran into this before, it must be something extremely common, I just program too much in Java and that language hides most of this ugliness from us...
// Create vector
vector<CMyClass> vObjects;
// Add a whole bunch of stuff
........
// Now insert something in the middle
vObjects.insert (&vObjects[3], new CMyClass);
That last line causes an error. It can't convert the pointer to a reference. So I try adding a & or * to it along with a mixture of parenthesis in the vain hope that something will click, but nothing. How do I get a reference out of that pointer? I know I ran into this before, it must be something extremely common, I just program too much in Java and that language hides most of this ugliness from us...
ASKER
iterator insert (iterator i, const T &val);
Hi,
So you should supply iterator as a first parameter. Examples:
vObjects.insert(vObjects.b egin(), new CMyClass) -- inserts as a first element.
vector<int>::iterator it = Objects.begin();
advance(it,3); // Now it points to a third element
vObjects.insert (it, new CMyClass); //Inserts before the third element
Regards,
Igor
So you should supply iterator as a first parameter. Examples:
vObjects.insert(vObjects.b
vector<int>::iterator it = Objects.begin();
advance(it,3); // Now it points to a third element
vObjects.insert (it, new CMyClass); //Inserts before the third element
Regards,
Igor
With a vector, you can do:
vObjects.insert (vObjects.begin()+3, new CMyClass);
vObjects.insert (vObjects.begin()+3, new CMyClass);
It's a vector<CMyClass> not vector<CMyClass*>
so it should be:
vObjects(vObjects.begin()+ 3, *new CMyClass);
not:
vObjects(vObjects.begin()+ 3, new CMyClass);
In any case, it should be noticed that inserting in at vector at any place other than the end takes linear time (at the end constant time).
If you need this, you should think of switching to list, although you can't write vObject.begin() + 3 any more...
Luc
so it should be:
vObjects(vObjects.begin()+
not:
vObjects(vObjects.begin()+
In any case, it should be noticed that inserting in at vector at any place other than the end takes linear time (at the end constant time).
If you need this, you should think of switching to list, although you can't write vObject.begin() + 3 any more...
Luc
All of these seem to have memory leaks though.
You are allocating a new object with new and then storing a copy of that object in the vector<>. But you loose the pointer to the new object so it can never be deleted.
So you should instead do
vObjects.insert (vObjects.begin()+3, CMyClass());
or if there is a default constructor (whichI giess the above certainly indicates.) then
vObjects.insert (vObjects.begin()+3);
You are allocating a new object with new and then storing a copy of that object in the vector<>. But you loose the pointer to the new object so it can never be deleted.
So you should instead do
vObjects.insert (vObjects.begin()+3, CMyClass());
or if there is a default constructor (whichI giess the above certainly indicates.) then
vObjects.insert (vObjects.begin()+3);
ASKER
The one thing I am concerned about is memory. I need this to be rock-solid, since it will be juggling around anywhere from 100 kb to 20 mb of memory. Leaks are bad!
Anywhere, about your suggestion, nietod... if I delete the vector, won't it destroy all the objects it contains? And I can get that pointer back. If I know its index, I could just do this:
pObject = &vObjects[nIndex];
Anyway, I tried this:
vObjects.insert (vObjects.begin () + nIndex, CMyClass ());
It finds no problems with my code, but it gives me an error *inside* the vector! Basically it says it can't compile it because my class has no copy constructor... when I know it does! The copy constructor works elsewhere in the program, and I double and triple checked it with a C++ book.
Here is the prototype of the copy constructor, maybe I am overlooking something:
CMyClass (CMyClass &op);
Thanks for the help!
Anywhere, about your suggestion, nietod... if I delete the vector, won't it destroy all the objects it contains? And I can get that pointer back. If I know its index, I could just do this:
pObject = &vObjects[nIndex];
Anyway, I tried this:
vObjects.insert (vObjects.begin () + nIndex, CMyClass ());
It finds no problems with my code, but it gives me an error *inside* the vector! Basically it says it can't compile it because my class has no copy constructor... when I know it does! The copy constructor works elsewhere in the program, and I double and triple checked it with a C++ book.
Here is the prototype of the copy constructor, maybe I am overlooking something:
CMyClass (CMyClass &op);
Thanks for the help!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yeah yeah yeah... looks like I overlooked something just a little bit important! Once I fixed the copy constructor it worked fine!
alok