vector insert

May 8, 2009 at 5:49pm
I want to insert an object into a std::vector, but do not want to change the capacity of the vector. I believe insert will try to increase the size of the vector(is that right??). How is this accomplished?

Which brings up another question: does <vector> address "size" of the actual number of elements versus "capacity", (max capacity) of a vector?
May 8, 2009 at 6:07pm
You should not be concerned by the current capacity of the vector.

A vector is stored something like (simplified):
1
2
3
4
5
6
7
template <typename T>
class vector
  {
  T*       data;      // The allocated space for the array of type T 
  unsigned size;      // The number of items in use in data[]
  unsigned capacity;  // The number of items available in data[]
  ...

The capacity is only increased when there is need to insert more items than there is room.


But again, don't worry about the capacity -- it will properly take care of itself.

Hope this helps.
May 8, 2009 at 6:32pm
Use reserve() to set the capacity of the vector before you start adding items to it. There are legitimate reasons to avoid changing the capacity of a vector, but they are rarely on topic in the beginner forum.
May 8, 2009 at 6:38pm
Thanks for the help Duoas. This is for an assignment where the vector is a class variable that will maintain a constant capacity. If I call vector's insert function, will it mess with the capacity?

I've already created code that will put an object at an index and will manually move elements to the right of the insertion point over one spot, but I didn't know if I could simply use "insert." Same for "erase" when removing an object.
May 8, 2009 at 6:54pm
Sounds like you really just want an array, not a vector.
May 8, 2009 at 7:11pm
Resorting to an array would be nifty, unfortunately the assignment calls for a vector. Drat.

I'll probably just stick manual insertion using []. After all, the directions don't specify the insertion method, but I thought I'd try to utilize existing (and already included) code...
Last edited on May 8, 2009 at 7:14pm
May 8, 2009 at 8:00pm
Hmm, sounds like PanGalactic has the answer you need for your assignment. Check with your professor.
May 8, 2009 at 8:34pm
Can't get in touch with him right now. One last thing, then I'll stop bothering you guys about this:

PanGalactic: I just wanted to be sure that using reserve() as you've recommended will indeed prevent insert() or erase() from changing the capacity of the vector?
May 8, 2009 at 8:39pm
Have a small read of - http://cplusplus.com/reference/stl/vector/reserve/ - It may clear a few things up and/or help you understand and gain more knowledge of the vector container.
May 8, 2009 at 8:41pm
VictorH: it will provided you do not exceed the capacity you have reserved.

See http://www.sgi.com/tech/stl/Vector.html especially footnotes 4 & 5.
Last edited on May 8, 2009 at 9:04pm
May 8, 2009 at 8:48pm
You have a ',' at the end of the site name. Remove it and the link will work :)
May 8, 2009 at 8:50pm
Thanks everyone. I appreciate the assist.
Topic archived. No new replies allowed.