Advertisement

help using insert() on a vector

Started by November 20, 2002 05:57 AM
0 comments, last by Shenron 21 years, 11 months ago
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]
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."

-----------------------------"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."

This topic is closed to new replies.

Advertisement