Iterator pair as a function argument?

Discussion in 'C++' started by nvangogh, Feb 19, 2014.

  1. nvangogh

    nvangogh Guest

    Suppose I have a vector<int> values. Now the pair values.begin() and
    values.end() will cover the range of the vector.

    I am writing a simple function that takes the pair of iterators as
    arguments along with an int value. The function then returns a bool to
    indicate if the int value is found in the vector.

    The first problem I have is to question if it is possible to pass
    iterators as distinct arguments to a function? I would have thought that
    the only way to accomplish this would be to pass the vector as a
    reference, from there the function can use the iterators to do it's work.

    I was thinking the correct definition would be

    bool find_value (vector<int>&, int);

    Or is there a way to pass an iterator pair as function arguments which I
    have missed?
     
    nvangogh, Feb 19, 2014
    #1
    1. Advertisements

  2. nvangogh

    Jorgen Grahn Guest

    Except not returning a bool. I might write a small wrapper
    (untested):

    template<class It, class Val>
    bool contains(It a, It b, const Val& val) {
    return std::find(a, b, val)!=b;
    }

    Sometimes the code becomes much more readable as
    if(contains(a, b, 42) && !contains(b, c, 96)) ...
    instead of
    if(std::find(a, b, 42)!=b && std::find(b, c, 96)==c) ...

    /Jorgen
     
    Jorgen Grahn, Feb 19, 2014
    #2
    1. Advertisements

  3. nvangogh

    Jorgen Grahn Guest

    I recall being confused because I assumed my code would automatically
    be generic (work with any container) because I used containers and
    iterators. And yet I had to pick /one/ type -- unless I wrote a
    template function, which sounded too advanced for me.

    After a while I realized genericity of /my/ code wasn't the goal.
    And that most code can choose one suitable container (often
    std::vector) and stick to it.
    I used a book on STL, but I would probably have been better off
    reading the online SGI STL manual. That one was very good (for me).

    /Jorgen
     
    Jorgen Grahn, Feb 24, 2014
    #3
  4. Hi nvangogh
    To me everything becomes so much clearer if I use typedef

    typedef std::vector<int>TIntVec;
    typedef TIntVec::iterator TIntVecIter;

    bool foo( TIntVecIter Begin, TIntVecIter End, int )

    Best regards
    Asger-P
    http://Asger-P.dk/software
     
    Asger Joergensen, Mar 19, 2014
    #4
  5. nvangogh

    Jorgen Grahn Guest

    Your news agent has distorted the text by "nvangogh" you're quoting --
    dropping '>' among other things, it seems. Please switch to some
    non-broken software! People might think nvangogh writes nonsense, if
    they don't go back and check the original.

    /Jorgen
     
    Jorgen Grahn, Mar 19, 2014
    #5
  6. Hi Jorgen

    Very sorry about that, unfortunately I can't blame the news reader for that
    It is a bug in my own script, I use for breaking lines at a readable length.
    I will fix it right away.
    Thanks for letting me know.

    Best regards
    Asger-P
     
    Asger Joergensen, Mar 19, 2014
    #6
  7. nvangogh

    Jorgen Grahn Guest

    Ah, then we can blame the OP a bit for using too long lines!

    (I had to break one of his lines too when responding to you,
    but I do that manually, and don't try hard to make it look
    nice. Been thinking about making my editor help me, but never
    got around to it.)

    /Jorgen
     
    Jorgen Grahn, Mar 20, 2014
    #7
    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.