# Thread: end of the vector

1. ## end of the vector

Hi guys, I'm not understanding why if I want to initiate an array and pass over it, I should reach array.end() and not reaching the last element .. why?! I mean it's not efficient to reach the array.end() if I could stop in the last element in the vector without reaching the array.end() ! ..any clue why we are using array.end()?

thanks alot

2. Write a loop condition and it'll become obvious.

3. Originally Posted by laserlight
Write a loop condition and it'll become obvious.
I know array.end() is the element after the last element, but my question is why I need to reach to the element after last element, I can just reach the last element ..without reaching to the element that's after last element which is array.end()

loop condition, whatever? it wouldn't help ... because I already know at array.end() we stop but my question why specifically we need this? we could just need the last element ..without the element after last element

4. Post your code.

5. Originally Posted by RyanC
loop condition, whatever? it wouldn't help ...
Despite trying to program for years, you still don't even have the sense to try writing a loop with end() and another loop with end() - 1 and investigate the differences. If you had done that, you would understand why end() returns a one-past-the-end iterator instead of an iterator to the actual last element, but instead you chose to just say "it wouldn't help". Of course it wouldn't help: you didn't even try it.

Originally Posted by RyanC
we could just need the last element ..without the element after last element
By definition, there is no element after the last element. The point of a one-past-the-end iterator is to make it easy to iterate over the entire range, i.e., you keep looping as long as you aren't one-past-the-end. It also allows you to elegantly define an empty range, i.e., the range in which begin() == end(). But of course, this doesn't help. Nothing helps someone who doesn't actually want to learn how to program.

Popular pages Recent additions