How is the implementation of vector::insert() ?

Hello everyone, I have problems with my implementation of std::vector, specifically with the function
vector::insert(iterator position, T value){}
and I don't know how to do the function.
If you can give the code or a simple idea, it's would be perfect.
pd: sorry about my english, is really bad.
thanks, but i don´t know how to do that, the code, i don´t know how to work with iterators
i don´t know how to work with iterators

Then I suggest you learn. Knowledge of iterators is critical to using C++ effectively.

I gave an example of inserting into a a vector using an iterator in this post:
http://www.cplusplus.com/forum/beginner/148708/#msg778932

You're not answering OP's question. He doesn't want to use std::vector. He's got to implement his own vector.

Remember that a vector wraps a plain old array, allocated with new[] on the heap. So at minimum your vector must have a constructor that calls new[] and a destructor that calls delete[].

When given an iterator (which in your case, should either be a pointer or an integer index into the array, right?) and a value to insert, what you are being asked is akin to: how do I insert a value into my array?

Secondly, your vector class should, as usual, have two numbers describing the array: the number of elements available (which is the size of the allocated array), and the number of elements used (which is what the user things the size of the array is). [std::vector calls these 'capacity' and 'size'.]

number_of_elements_available = 9
number_of_elements_used      = 7
index 0  1  2  3  4  5  6  7  8
value 7  19 -3 24 8  44 73 ?  ?
            ^ insert element 15 at index 2

Makes:
number_of_elements_available = 9
number_of_elements_used      = 8
index 0  1  2  3  4  5  6  7  8
value 7  19 15 -3 24 8  44 73 ?

Do you see what changed?

The first question to ask yourself is: are there enough elements available to insert a new value? (If not, you'll have to new[] another array, just with more elements available, copy the elements of the current array over, and delete[] the old array.

The next thing to do is to move items [2 through number_of_elements_used-1] over to [3 through number_of_elements_used]. (This is a problem you should have solved previously for your class when playing with arrays.)

The last thing is to assign element 2 the new value 15.

If all that is beyond you, you'll need to go get help from your professor. Sorry.

Hope this helps.
Topic archived. No new replies allowed.