Iterator pair as a function argument?

N

nvangogh

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?
 
J

Jorgen Grahn

....
There are plenty of STL functions like std::sort() which take iterators as
arguments, maybe you can take a look of their interfaces? BTW, your
function appears to be already implemented as std::find(), with iterator
arguments.

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
 
J

Jorgen Grahn

It has been a while, but I recall iterators being a little puzzling
when I first encountered them. It took a little while to click
that foo::iterator is just another type. I can't say what I thought
they were, but they were mildly confusing.

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.
This was from the context of learn-by-using. If I'd started with
a good explanation/description, that confusion might not have been
there.

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
 
A

Asger Joergensen

Hi nvangogh
Suppose I have a vector<intvalues. 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?

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
 
J

Jorgen Grahn

Hi nvangogh

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
 
A

Asger Joergensen

Hi Jorgen

Jorgen said:
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.

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
 
J

Jorgen Grahn

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.

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top