inserting elements in existing vector

hi everyone,

I have a question about inserting values in a vector that already has some elements.

Suppose a vector of integers "a" has 5 existing elements i.e. a[0],a[1].....a[4]. Now I want to add, say 3 values between a[0] and a[1]. That means the vector should become a[0],a[1],a[2],a[3],a[4].....a[7]. Here a[1],a[2] and a[3] are 3 new values I wanted to add and old a[1] (before modification ) should become a[4].

Thanks for your time and help.

Who is Participating?
Sys_ProgConnect With a Mentor Commented:
AFAIK, there is no other way of inserting an element into a vector other than using insert()

Refer the foll. link for reference to vector

The following link gives a complete description of vectors and their use/purpose. It also has links for list, deque, slist at the end of page


abybabyAuthor Commented:
hi everyone,

well, I solved problem myself but still I would appreciate if you have any other method of dealing the problem which is more efficient.
Also, I have a question. Is it better to do all the insertion operation using a list and then copy the values in a vector for later use in the program.

here is the solution code.

typedef vector<int> INT;
int main()

      INT a;
      int index=1;
      INT::iterator i,where;

      for (i=a.begin();i!=a.end();i++)
              return 1;
Inserting in the middle of a vector is always expensive
Why don't u use list instead of a vector

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.