Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Iterator pair as a function argument?

105 views
Skip to first unread message

nvangogh

unread,
Feb 19, 2014, 3:22:47 PM2/19/14
to
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?

Ian Collins

unread,
Feb 19, 2014, 3:23:27 PM2/19/14
to
std::find?

--
Ian Collins

Paavo Helde

unread,
Feb 19, 2014, 3:28:53 PM2/19/14
to
nvangogh <nvan...@pcexpert.net> wrote in news:cn8Nu.5082$1B3...@fx26.am4:
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.

Cheers
Paavo

Jorgen Grahn

unread,
Feb 19, 2014, 4:03:33 PM2/19/14
to
On Wed, 2014-02-19, Paavo Helde wrote:
> nvangogh <nvan...@pcexpert.net> wrote in news:cn8Nu.5082$1B3...@fx26.am4:
...
>> bool find_value (vector<int>&, int);
...
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Juha Nieminen

unread,
Feb 24, 2014, 6:12:16 AM2/24/14
to
nvangogh <nvan...@pcexpert.net> wrote:
> The first problem I have is to question if it is possible to pass
> iterators as distinct arguments to a function?

Why wouldn't it be? What exactly is the problem you are envisioning
with it?

bool foo(std::vector<int>::iterator b, std::vector<int>::iterator e, int)

Of course if you want to make the function more generic you should
templatize it so that it will accept iterators of any type of vector
(or even any other data container.)

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

Drew Lawson

unread,
Feb 24, 2014, 9:49:09 AM2/24/14
to
In article <lef9ed$2hos$1...@adenine.netfront.net>
Juha Nieminen <nos...@thanks.invalid> writes:
>nvangogh <nvan...@pcexpert.net> wrote:
>> The first problem I have is to question if it is possible to pass
>> iterators as distinct arguments to a function?
>
>Why wouldn't it be? What exactly is the problem you are envisioning
>with it?

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.

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.


>bool foo(std::vector<int>::iterator b, std::vector<int>::iterator e, int)
>
>Of course if you want to make the function more generic you should
>templatize it so that it will accept iterators of any type of vector
>(or even any other data container.)
>
>--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---


--
Drew Lawson

". . . And I never give a reason"
-- God, as channeled by Seven Nations

Jorgen Grahn

unread,
Feb 24, 2014, 12:55:29 PM2/24/14
to
On Mon, 2014-02-24, Drew Lawson wrote:
> In article <lef9ed$2hos$1...@adenine.netfront.net>
> Juha Nieminen <nos...@thanks.invalid> writes:
>>nvangogh <nvan...@pcexpert.net> wrote:
>>> The first problem I have is to question if it is possible to pass
>>> iterators as distinct arguments to a function?
>>
>>Why wouldn't it be? What exactly is the problem you are envisioning
>>with it?
>
> 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).

Asger Joergensen

unread,
Mar 19, 2014, 6:28:42 AM3/19/14
to
Hi nvangogh

nvangogh wrote:

> 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


Jorgen Grahn

unread,
Mar 19, 2014, 5:48:09 PM3/19/14
to
On Wed, 2014-03-19, Asger Joergensen wrote:
> Hi nvangogh
>
> nvangogh wrote:
>
>> 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);

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.

Asger Joergensen

unread,
Mar 19, 2014, 6:45:13 PM3/19/14
to
Hi Jorgen

Jorgen Grahn wrote:

> 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
--
http://Asger-P.dk/software

Jorgen Grahn

unread,
Mar 20, 2014, 3:11:29 AM3/20/14
to
On Wed, 2014-03-19, Asger Joergensen wrote:
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.)
0 new messages