Thread: end of the vector

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    Write a loop condition and it'll become obvious.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,418
    Post your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    Quote 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.

    Quote 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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-21-2016, 09:38 AM
  2. Replies: 3
    Last Post: 02-09-2015, 09:56 PM
  3. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  4. Replies: 4
    Last Post: 07-05-2013, 02:30 AM
  5. Simple Question memset(vector<int>, 0, sizeof(vector<int>))
    By HyperShadow in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 04:56 PM

Tags for this Thread