Solved

inserting elements in existing vector

Posted on 2004-04-01
5
180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

732 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