Solved

inserting elements in existing vector

Posted on 2004-04-01
5
176 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
5 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 50 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

679 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