Solved

inserting elements in existing vector

Posted on 2004-04-01
5
169 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

746 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

10 Experts available now in Live!

Get 1:1 Help Now