question on vector<T>::end() function

I am trying to write my own version of STL vector<T> class.
I have troubles on the end() function;


for example:

1
2
3
4
5
MyVec<int> vec;
.... //put some value in vec

MyVec<int>:: iterator itr = vec.end();
itr--;



the vec.end() should return a NULL value, but the itr-- should return the last value in the vector. Is there any easy way to write this?


thanks ~!
Why not just use vec.back()?
Why should your vec.end() return a NULL? That's not what the STL or the C++ vector does.
@cipermagi, back() returns the last element, end() returns one past the last element.

Otherwise, in your class just set a pointer to the last element in your array, and then increment it by one? Also, itr-- doesn't return one past the last element, it returns the last. You would have to use --itr.
@ascii: The reason that I suggested using back() is because it appears that the point of putting it at end() and then immediately pushing it towards the front by one is to...return the last element.
*facepalm*, my bad :)

In that case, take ciphermagi's advice, and just use back().
Last edited on
@ciphermagl, @ascii, thank you for you advices.

I know that vec.back() will do the job. But in this assignment we are going to write whole MyVec class, including every member function(begin(), end(),back()....). In other words, if you are the person who writes STL end() function, how will you write it?

Assuming all the values in your class are stored in an array (which makes sense), I would set a pointer to the last element in the array, and increment it by one. Then return the pointer.
Ok if you check what back() and end() of std::vector returns you get:

back():
A reference to the last element in the vector.
Unlike member vector::end, which returns an iterator just past this element, this function returns a direct reference.

end()
An iterator to the element past the end of the sequence.
Notice that unlike member vector::back, which returns a reference to the element preceding this one, this function returns a random access iterator.

So besides the fact that they point to different places they return different types.
http://www.cplusplus.com/reference/stl/vector/end/
http://www.cplusplus.com/reference/stl/vector/back/

Also I don't think end() returns NULL at some point.

What ever implementation you are using just make sure that with end() you return an iterator (in your case a random access pointer as you want vector API implemented) on past the last valid element of your vector. That means you must make sure operator[], ++, or pointers arithmetic all points to the same element.

Putting your values it in an array is probably the simplest to do.
I would imagine that your internal iterator class stores a pointer? I would then just return an iterator with its pointer set to one past the last element.

EDIT:

Note: vec.end(); should not return a NULL value. It should return an iterator object that indicates one past the last element, however you choose to represent that internally..
Last edited on
@eypros, all this really means is that you can't call end() on a list, which makes sense.
Topic archived. No new replies allowed.