does this invoke undefined behaviour

S

subramanian100in

I assign an initial iterator value returned by 'find()' algorithm to a
variable called 'first'.
Container::const_iterator first = find(c.begin(), c.end(), value);

Then I want to find the next occurrence of the 'value' in the sequence
to 'first'. To do this, I write the following code.

if (first != c.end())
{
first = find(++first, c.end(), value);
// do something with 'first'
}

Is the above assignment correct ? Or does it invoke undefined
behaviour because I do an increment of 'first' inside 'find()'
algorithm and store the result returned by 'find()' in 'first'
itself ?

Should I equivalently write the following piece of code ?

Container::const_iterator temp;
if (first != c.end())
{
temp = find(++first, c.end(), value);
// do something with 'temp'
}

Kindly clarify.

Thanks
V.Subramanian
 
R

Rolf Magnus

I assign an initial iterator value returned by 'find()' algorithm to a
variable called 'first'.
Container::const_iterator first = find(c.begin(), c.end(), value);

Then I want to find the next occurrence of the 'value' in the sequence
to 'first'. To do this, I write the following code.

if (first != c.end())
{
first = find(++first, c.end(), value);
// do something with 'first'
}

Is the above assignment correct ?
Yes.

Or does it invoke undefined behaviour because I do an increment of 'first'
inside 'find()' algorithm and store the result returned by 'find()' in
'first' itself ?

No. Due to the function call, there is a sequence point in between.
 
J

James Kanze

No. Due to the function call, there is a sequence point in
between.

On the other hand, modifying the same variable twice in a single
expression is confusing to the reader. I'd probably use
something like the Pascal succ function:

template< typename Iterator >
Iterator
succ( Iterator iter )
{
++ iter;
return iter;
}

and write
first = find( succ( first ), c.end(), value );

(In practice, every time I've needed this, I've been dealing
with string iterators, so I'd just write:
first = find( first + 1, c.end(), value );
No point in making things overly complicated when it doesn't buy
you anything.)
 
J

Jeff Flinn

James said:
On the other hand, modifying the same variable twice in a single
expression is confusing to the reader. I'd probably use
something like the Pascal succ function:

template< typename Iterator >
Iterator
succ( Iterator iter )
{
++ iter;
return iter;
}

and write
first = find( succ( first ), c.end(), value );

(In practice, every time I've needed this, I've been dealing
with string iterators, so I'd just write:
first = find( first + 1, c.end(), value );
No point in making things overly complicated when it doesn't buy
you anything.)

Or if you're already using boost that's spelled:

#include <boost/next_prior.hpp>

first = find( boost::next( first ), c.end(), value );

Jeff
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top