Archived

This topic is now archived and is closed to further replies.

help using insert() on a vector

This topic is 5500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to use insert to copy the contents of vector 1 at a time to the front of the same vector. It has to be at the beginning of a vector and has to be inserted 1 element at a time. This is what I have been messing around with but I can't get it to work. Any help is greatly appreciated.
    
void copy(vector& v)
{
     vector::iterator i = v.begin();

     while(i != v.end())
          i = v.insert(v.begin(), *i);
}
      
I've also tried using ++i; and i+= 2;. From what I've read inserting a element into the beginning of a vector will invalidate the iterator but the insert() function should return a valid iterator therefore I set i equal to it but it doesn't work either. I'm stumped. [edited by - Shenron on November 20, 2002 11:48:17 AM]

Share this post


Link to post
Share on other sites
try this:


int main()
{
vectorv1,v2;

copy(v1.begin(), v1.end(), back_inserter(v2));

}



if u must insert in the front of a vector and the vector is not empty maybe u should use a list.


-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

Share this post


Link to post
Share on other sites