std::vector<>::iterator and recognizable bounds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rune Allnor

    std::vector<>::iterator and recognizable bounds

    Hi folks.

    I have a function that takes an element in a vector as
    argument. The naive interface goes as

    float computeSomethin g(const std::vector<flo at>& v, size_t i)
    {
    size_t j = i-1;
    size_t k = i+1;

    if (j<0){/* Handle start boundary condition */}
    if (k>=v.size()){/* Handle terminal boundary condition */}
    }

    I would like to implement this in terms of iterators.
    But it seems one needs several iterators to get this
    done:

    typedef std::vector<flo at>::const_iter ator pos;

    float computeSomethin g(pos i, pos b, pos e)
    // Call function with b=v.begin(), e=v.end()
    {
    pos j = i; --j;
    pos k =i; ++k;

    if (i==b){/* Handle start boundary condition */}
    if (k==e){/* Handle terminal boundary condition */}
    }

    So my question is: Do there exist generally recognizable
    invalid values for the std::vector<>:: iterators? Like std::string::np os
    () ?

    If yes I could make a greatly simplified (as well as
    safer) interface to the function:

    float computeSomethin g(pos i)
    {
    pos j=i;--j;
    pos k = i;++k;

    if(j==std::vect or::npos){}
    if(k==std::vect or::npos) {}
    }

    But I can't find any analogy to std::string::np os defined for
    vectors. Does such a value exist?

    Rune
  • Pete Becker

    #2
    Re: std::vector&lt; &gt;::iterat or and recognizable bounds

    On 2008-11-20 08:42:01 -0500, Rune Allnor <allnor@tele.nt nu.nosaid:
    >
    So my question is: Do there exist generally recognizable
    invalid values for the std::vector<>:: iterators? Like std::string::np os
    () ?
    No. Iterators designate sequences, so they usually come in pairs.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)

    Comment

    • KK

      #3
      Re: std::vector&lt; &gt;::iterat or and recognizable bounds

      On Nov 20, 5:42 am, Rune Allnor <all...@tele.nt nu.nowrote:
      Hi folks.
      >
      I have a function that takes an element in a vector as
      argument. The naive interface goes as
      >
      float computeSomethin g(const std::vector<flo at>& v, size_t i)
      {
         size_t j = i-1;
         size_t k = i+1;
      >
         if (j<0){/* Handle start boundary condition */}
         if (k>=v.size()){/* Handle terminal boundary condition */}
      >
      }
      >
      I would like to implement this in terms of iterators.
      But it seems one needs several iterators to get this
      done:
      >
      typedef std::vector<flo at>::const_iter ator pos;
      >
      float computeSomethin g(pos i, pos b, pos e)
      // Call function with b=v.begin(), e=v.end()
      {
          pos j = i; --j;
          pos k =i; ++k;
      >
          if (i==b){/* Handle start boundary condition */}
          if (k==e){/* Handle terminal boundary condition */}
      >
      }
      >
      So my question is: Do there exist generally recognizable
      invalid values for the std::vector<>:: iterators? Like std::string::np os
      () ?
      >
      If yes I could make a greatly simplified (as well as
      safer) interface to the function:
      >
      float computeSomethin g(pos i)
      {
          pos j=i;--j;
          pos k = i;++k;
      >
          if(j==std::vect or::npos){}
          if(k==std::vect or::npos) {}
      >
      }
      >
      But I can't find any analogy to std::string::np os defined for
      vectors. Does such a value exist?
      >
      Rune
      /* apply const attribute where applicable */
      float computeSomethin g(vector<floatv ec, vector<float>:: iterator
      iter)
      {
      vector<float>:: iterator fIter = iter, bIter =iter;
      if (iter != vec.begin() || iter != vec.end())
      {
      advance(fIter,+ 1);
      advance(bIter,-1);
      }
      else {/* fill this up */ }
      if (bIter == vec.begin())
      {/* Handle start boundary condition */
      }
      if (fIter == vec.end())
      {/* Handle terminal boundary condition */
      }
      }

      Comment

      • James Kanze

        #4
        Re: std::vector&lt; &gt;::iterat or and recognizable bounds

        On Nov 20, 2:42 pm, Rune Allnor <all...@tele.nt nu.nowrote:
        I have a function that takes an element in a vector as
        argument. The naive interface goes as
        float computeSomethin g(const std::vector<flo at>& v, size_t i)
        {
        size_t j = i-1;
        size_t k = i+1;
        if (j<0){/* Handle start boundary condition */}
        if (k>=v.size()){/* Handle terminal boundary condition */}
        }
        I would like to implement this in terms of iterators. But it
        seems one needs several iterators to get this done:
        Yep. That's part of the design of iterators---never use one
        parameter when two can do. It makes a lot of things very
        painful.
        typedef std::vector<flo at>::const_iter ator pos;
        float computeSomethin g(pos i, pos b, pos e)
        // Call function with b=v.begin(), e=v.end()
        {
        pos j = i; --j;
        pos k =i; ++k;
        >
        if (i==b){/* Handle start boundary condition */}
        if (k==e){/* Handle terminal boundary condition */}
        }
        That's more or less an exact equivalent of your original
        version, yes. Now if you make your function a template, it will
        work with any sequence which is defined by bidirectional
        iterators.
        So my question is: Do there exist generally recognizable
        invalid values for the std::vector<>:: iterators? Like
        std::string::np os () ?
        No.
        If yes I could make a greatly simplified (as well as
        safer) interface to the function:
        float computeSomethin g(pos i)
        {
        pos j=i;--j;
        pos k = i;++k;
        if(j==std::vect or::npos){}
        if(k==std::vect or::npos) {}
        }
        In other words, you want the iterator to encapsulate both the
        current position and both upper and lower bounds somehow. It's
        certainly possible, but I'm not sure what you'd gain by it.
        But I can't find any analogy to std::string::np os defined for
        vectors. Does such a value exist?
        No, although you could probably define one arbitrarily.
        Something like static_cast< size_t >( -1 ). Like
        std::string::np os, it would be an index, however, and not an
        iterator.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...