Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

inserting elements in existing vector

Posted on 2004-04-01
5
Medium Priority
?
193 Views
Last Modified: 2010-04-01
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.

abybaby.
0
Comment
Question by:abybaby
  • 2
3 Comments
 

Author Comment

by:abybaby
ID: 10738439
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;
      a.push_back(0);
      a.push_back(1);
      a.push_back(2);
      a.push_back(3);
      a.push_back(4);
      int index=1;
      INT::iterator i,where;
      where=a.begin()+index;
      a.insert(where,10);
                where++;
      a.insert(where,11);

      for (i=a.begin();i!=a.end();i++)
      {
            cout<<(*i)<<endl;
      }
              return 1;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10738556
Inserting in the middle of a vector is always expensive
Why don't u use list instead of a vector

Amit
0
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 200 total points
ID: 10746658
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

http://www.cppreference.com/cppvector.html



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

http://www.sgi.com/tech/stl/Vector.html


Amit




0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

824 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