Solved

Inserting into a vector

Posted on 2000-02-21
9
261 Views
Last Modified: 2010-04-02
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...
0
Comment
Question by:TheSnowman
9 Comments
 
LVL 2

Expert Comment

by:alokanant
ID: 2544949
what is the prototype for the method insert?

alok
0
 

Author Comment

by:TheSnowman
ID: 2544983
iterator insert (iterator i, const T &val);
0
 
LVL 5

Expert Comment

by:proskig
ID: 2545033
Hi,
So you should supply iterator as a first parameter. Examples:
vObjects.insert(vObjects.begin(), 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



0
 
LVL 5

Expert Comment

by:yonat
ID: 2545263
With a vector, you can do:
vObjects.insert (vObjects.begin()+3, new CMyClass);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 2545516
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 2545751
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);
0
 

Author Comment

by:TheSnowman
ID: 2546733
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!
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2546867
>> if I delete the vector, won't it destroy
>> all the objects it contains?
Yes.  but  in

vObjects(vObjects.begin()+3, *new CMyClass);

the object you create with new is used only to initialze the object created inside the vector.  The object you create is not stored in the vector.  The vector creates its own object for storage..  so the object you created will still remain in memory, but not in the vector there is no way to ever find  it an no way to ever delete it.

>> And I can get that pointer back. If I know
>> its index, I could just do this:
>> pObject = &vObjects[nIndex];
No.  That is a pointer to a different object.  One that the vector created.  The one you created is handing around somewhere and can never be found agin.

>> Basically it says it can't compile it because
>> my class has no copy constructor.
That is not limited to the code I suggested.  If you will be storing this class inside the vector it must have a copy constructor.  so you must define one (or use the default one).   If the class cannot be copied for some reason, it cannot be stored directly in a vector.  But you can store pointers to obhjects of this class in a vector.  In that case, the way you were trying to add to the vector woudl be correct.  i.e. allocate an item with new and store the pointer in the vector.  The problem with this is that you have to remember to later delete these objects.  (if you store the objects directly, instead of pointers to objects, the vector will take care of the deleting.)

>> Here is the prototype of the copy constructor,
>> maybe I am overlooking something:
>> CMyClass (CMyClass &op);
That isn't a copy constructor, just a constructor.  A copy constructor takes a _constant_ reference parameter.  Yours isn;t constant.
0
 

Author Comment

by:TheSnowman
ID: 2547090
Yeah yeah yeah... looks like I overlooked something just a little bit important! Once I fixed the copy constructor it worked fine!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now