CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Unhappy stl std::iterator

    If you've got an iterator iter, how do you get an iterator pointing to the previous thing EASILY?

    Like eg
    Code:
    while (iter!= (iter0 - 1))
    No answers of the form

    That's easy, you just declare another iterator:
    Code:
    Iterator iter1;
    iter1 = iter;
    --iter1;
    0 points.

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    What is the significance of using a temporary variable? It will not affect the over performance.

    Kuphryn

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Naughty, Yves - that's a trick question, dropping forward iterators into it, when he's asked for the previous.
    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
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352

    Wink

    Originally posted by Yves M
    What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
    Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
    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


  7. #7
    Join Date
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352
    Originally posted by Graham
    It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
    No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.

    --iter is, like iter = iter-1;

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by c94wjpn
    Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?
    Well, because making a map's iterator random access would make you incurr big performance penalties. The ++ and -- operators for map's iterators are already amortized constant time, so it's a little slower than constant time. Getting a random access iterator over a map would be amortized linear time (so something like O(N log(log(N))) ).

    It just doesn't make sense. Anyways, I don't see the problem in having a temporary iterator which points at the previous element.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by c94wjpn
    No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.

    --iter is, like iter = iter-1;
    Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
    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
    Mar 2000
    Location
    Oxford, UK.
    Posts
    352
    Originally posted by Graham
    Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
    ok thanks.

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