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

Dec 27, 2011 at 7:55pm
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 ~!
Dec 27, 2011 at 7:58pm
Why not just use vec.back()?
Dec 27, 2011 at 8:17pm
Why should your vec.end() return a NULL? That's not what the STL or the C++ vector does.
Dec 27, 2011 at 11:16pm
@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.
Dec 27, 2011 at 11:28pm
@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.
Dec 27, 2011 at 11:33pm
*facepalm*, my bad :)

In that case, take ciphermagi's advice, and just use back().
Last edited on Dec 27, 2011 at 11:33pm
Dec 28, 2011 at 2:03am
@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?

Dec 28, 2011 at 4:53am
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.
Dec 28, 2011 at 3:17pm
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.
Dec 28, 2011 at 3:24pm
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 Dec 28, 2011 at 3:27pm
Dec 28, 2011 at 10:38pm
@eypros, all this really means is that you can't call end() on a list, which makes sense.
Topic archived. No new replies allowed.