CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181

    How to get the iterator of the last element?

    Except using rbegin, are there any other stl functions for getting the iterator of the last element? vector::back just gives me the reference of that.
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    --c.end().

    Only works for Bidirectional containers but, then, so does c.rbegin().
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Graham,

    Isn't c.end () one past the last one?
    Succinct is verbose for terse

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    That's why I applied the decrement operator to it.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Sorry - I thought the -- was a comment. Ada is getting me confused.
    Succinct is verbose for terse

  6. #6
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    If i use decrement operator on the end iterator, will the end iterator "itself" decrease by 1?

    Just like
    int i = 0;
    --i;
    Then i will become -1.

    May be this is a silly question, sorry.
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    No.

    end() simply returns a one-past-the-end iterator and it's that that you decrement.

    Code:
    std::vector<int> v;
    
    // fill v...
    
    std::vector<int>::iterator i = --v.end();
    
    // i now points to the last element in v.
    It would probably be a good idea to check that the container is not empty(), though. Decrementing end() then could cause problems.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    Oops, VC6 can't compile it.

    error C2105: '--' needs l-value
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    OK. Get the interator into a variable and decrement that:
    Code:
    (whatever)::iterator i = c.end();
    --i;
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Jun 1999
    Location
    Hong Kong
    Posts
    181
    Thanks
    In Chinese Proverb, "Teaching the poor fishing is better than giving fish to them".

  11. #11
    Join Date
    May 2001
    Posts
    472
    You can also write
    Code:
    (c.end() - 1)
    for vector iterators.
    Ce n'est que pour vous dire ce que je vous dis.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured