next iterator position without mutation

Discussion in 'C++' started by Cagdas Ozgenc, Sep 1, 2003.

  1. Greetings.

    Is there a convinient way to get the next iterator position without mutating
    the iterator for non-random access containers? For example,

    i + 1

    instead of

    i++

    Thanks
     
    Cagdas Ozgenc, Sep 1, 2003
    #1
    1. Advertisements

  2. Cagdas Ozgenc

    Attila Feher Guest

    Nope. For input or output iterators you cannot even read the same place
    twice. I might be wrong, but for forward or bidirectional iterators you may
    copy the iterator and increment the copy to "take a peek".
     
    Attila Feher, Sep 1, 2003
    #2
    1. Advertisements

  3. Nope. For input or output iterators you cannot even read the same place
    That's bad news. The following code would be quite dangerous then:

    template<typename Iterator>
    Iterator operator+(Iterator const& input,size_t offset) {
    Iterator output = input;
    while(offset > 0) {
    ++output;
    offset--;
    }
    return output;
    }

    I can't find a forward_iterator class to make the above function iterator
    type-safe. What's wrong with these STL iterators?

    Thanks
     
    Cagdas Ozgenc, Sep 1, 2003
    #3
  4. Cagdas Ozgenc

    Attila Feher Guest

    You have iterator traits, OMHO you can make a compile time assertion if the
    function template is being instantiated with the wrong iterator category.
     
    Attila Feher, Sep 1, 2003
    #4
  5. You have iterator traits, OMHO you can make a compile time assertion if
    the
    Can you give a small example, or give pointers on that matter.

    Thanks
     
    Cagdas Ozgenc, Sep 1, 2003
    #5
  6. Cagdas Ozgenc

    Attila Feher Guest

    Attila Feher, Sep 1, 2003
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.