Solved

inserting elements in existing vector

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 clear a vector as well as how to detect empty vectors in C++.

932 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

9 Experts available now in Live!

Get 1:1 Help Now