Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why next/prev for iterators in C++0x?

Reply
Thread Tools

Why next/prev for iterators in C++0x?

 
 
Scott Meyers
Guest
Posts: n/a
 
      04-23-2011
I just noticed that the FDIS (and at least some older drafts) includes
specifications for next and prev funtions on iterators. They're just
shorthands for calls to advance:

template <class ForwardIterator>
ForwardIterator next(ForwardIterator x,
typename std::iterator_traits<ForwardIterator>::difference_ type n = 1);

Effects: Equivalent to advance(x, n); return x;


template <class BidirectionalIterator>
BidirectionalIterator prev(BidirectionalIterator x,
typename std::iterator_traits<BidirectionalIterator>::diffe rence_type n
= 1);

Effects: Equivalent to advance(x, -n); return x;

Does anybody happen to know the motivation for putting these functions
in the new standard?

Thanks,

Scott

--
* C++ and Beyond: Meyers, Sutter, & Alexandrescu, Aug 7-10 in Banff
(http://cppandbeyond.com/)
 
Reply With Quote
 
 
 
 
Vaclav Haisman
Guest
Posts: n/a
 
      04-23-2011
Scott Meyers wrote, On 23.4.2011 20:16:
> I just noticed that the FDIS (and at least some older drafts) includes
> specifications for next and prev funtions on iterators. They're just
> shorthands for calls to advance:
>
> [...]
>
> Does anybody happen to know the motivation for putting these functions in the
> new standard?

Aren't those the work horse of the for(T x: collection) statement?

--
VH
 
Reply With Quote
 
 
 
 
Scott Meyers
Guest
Posts: n/a
 
      04-23-2011
On 4/23/2011 11:31 AM, Vaclav Haisman wrote:
> Aren't those the work horse of the for(T x: collection) statement?


Nope, that relies on the ++ operator. Details in FDIS 6.5.4/1. From
what I can tell, there are zero uses of prev and next on iterators in
the FDIS.

Scott

--
* C++ and Beyond: Meyers, Sutter, & Alexandrescu, Aug 7-10 in Banff
(http://cppandbeyond.com/)
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      04-23-2011
Scott Meyers wrote:

> I just noticed that the FDIS (and at least some older drafts) includes
> specifications for next and prev funtions on iterators. They're just
> shorthands for calls to advance:
>
> template <class ForwardIterator>
> ForwardIterator next(ForwardIterator x,
> typename std::iterator_traits<ForwardIterator>::difference_ type n = 1);
>
> Effects: Equivalent to advance(x, n); return x;

[...]
> Does anybody happen to know the motivation for putting these functions
> in the new standard?


advance modifies its argument, whereas next(it) is a version of it+1
that works with more general iterators. To emulate next with advance,
you actually need to first copy the iterator, which can be
inconvenient. I can for instance access the last element of a sequence
through *prev(end(sequence)) in line instead of declaring:
auto iter_end=end(sequence); --iter_end;
....expression using *iter_end...

The boost prior/next functions are used quite a bit and a proof that
this is convenient.
 
Reply With Quote
 
Scott Meyers
Guest
Posts: n/a
 
      04-23-2011
On 4/23/2011 2:53 PM, Marc wrote:
> advance modifies its argument, whereas next(it) is a version of it+1
> that works with more general iterators. To emulate next with advance,
> you actually need to first copy the iterator, which can be
> inconvenient. I can for instance access the last element of a sequence
> through *prev(end(sequence)) in line instead of declaring:
> auto iter_end=end(sequence); --iter_end;
> ...expression using *iter_end...
>
> The boost prior/next functions are used quite a bit and a proof that
> this is convenient.


Thanks for the explanation.

Scott
--
* C++ and Beyond: Meyers, Sutter, & Alexandrescu, Aug 7-10 in Banff
(http://cppandbeyond.com/)
 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      04-24-2011
On Apr 23, 5:53*pm, Marc <(E-Mail Removed)> wrote:
> Scott Meyers *wrote:
> > I just noticed that the FDIS (and at least some older drafts) includes
> > specifications for next and prev funtions on iterators. *They're just
> > shorthands for calls to advance:

>
> > template <class ForwardIterator>
> > ForwardIterator next(ForwardIterator x,
> > typename std::iterator_traits<ForwardIterator>::difference_ type n = 1);

>
> > Effects: Equivalent to advance(x, n); return x;

> [...]
> > Does anybody happen to know the motivation for putting these functions
> > in the new standard?

>
> advance modifies its argument, whereas next(it) is a version of it+1
> that works with more general iterators. To emulate next with advance,
> you actually need to first copy the iterator, which can be
> inconvenient. I can for instance access the last element of a sequence
> through *prev(end(sequence)) in line instead of declaring:
> auto iter_end=end(sequence); --iter_end;
> ...expression using *iter_end...
>
> The boost prior/next functions are used quite a bit and a proof that
> this is convenient.


I must adamantly agree. I've found that I've rarely used advance, but
jump at the chance to use next. At least for me, my use cases
commonly need both the iterator being advanced from, and the advanced
iterator, later in the logic. And while it is easy enough to copy the
iterator being advanced from and then advance it, it is just
delightful to do those two steps in just one with next. There's just
so many contexts where one step works and two don't (example snippet
from my own code):

...
for (ForwardIterator1 j = std::next(i); j != last1; ++j)
if (pred(*i, *j))
++c1;
...

Howard
 
Reply With Quote
 
crea
Guest
Posts: n/a
 
      04-24-2011

"Howard Hinnant" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On Apr 23, 5:53 pm, Marc <(E-Mail Removed)> wrote:

" ...
for (ForwardIterator1 j = std::next(i); j != last1; ++j)
if (pred(*i, *j))
++c1;
"

Just a small comment: I just read an article to say , that we dont really
(always) need to do ++j, but can do j++. The reason is that the computer can
trim the code many times when it compiles. Also testing showed that there
was no big difference even if j++ was used. But I have to read the article
again...
On the other hand, I dont know why using ++j would be not good.


 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-24-2011
* crea, on 24.04.2011 10:02:
> "Howard Hinnant"<(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> On Apr 23, 5:53 pm, Marc<(E-Mail Removed)> wrote:
>
> " ...
> for (ForwardIterator1 j = std::next(i); j != last1; ++j)
> if (pred(*i, *j))
> ++c1;
> "
>
> Just a small comment: I just read an article to say , that we dont really
> (always) need to do ++j, but can do j++. The reason is that the computer can
> trim the code many times when it compiles. Also testing showed that there
> was no big difference even if j++ was used. But I have to read the article
> again...
> On the other hand, I dont know why using ++j would be not good.


For the built-in operators the only difference is (in general) that writing
"++j" states just what you want, while "j++" adds a meaningless request.

The standard library's iterators all support "j++" with reasonable semantics.
The basis is the last row of table 72 in C++98 §24.1.1/2. However, although a
smart compiler will probably optimize the implied creation of some expression
result object, this is not necessarily so for non-standard iterators.

Also, a non-standard iterator may simply not provide "j++" (after all there's
seldom any good reason to use it), and so at least templated code can be more
generally useable if it does not rely on iterators providing "j++".

And then, when you have adopted the notation "++j" for iterators in general,
it's just as well to do so also for raw pointers, yielding the C++ convention of
writing "++j" by default, reserving the postfix form for where it's needed.

The pressures work the other way for C programming. In C there are only raw
pointers. And there is a tradition, all the way back to K&R, of writing "j++",
so in C it can be most natural to write "j++" by default.


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-25-2011
On Apr 24, 9:02 am, "crea" <(E-Mail Removed)> wrote:
> "Howard Hinnant" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...
> On Apr 23, 5:53 pm, Marc <(E-Mail Removed)> wrote:


> " ...
> for (ForwardIterator1 j = std::next(i); j != last1; ++j)
> if (pred(*i, *j))
> ++c1;
> "


> Just a small comment: I just read an article to say , that we
> dont really (always) need to do ++j, but can do j++. The
> reason is that the computer can trim the code many times when
> it compiles. Also testing showed that there was no big
> difference even if j++ was used. But I have to read the
> article again...
> On the other hand, I dont know why using ++j would be not good.


In practice, there's no valid technical reason for preferring
one over the other---it's just a question of which one you like
best. Politically speaking, however... some noted authors have
claimed otherwise, and have influences a large number of
programmers; it's easier to just use ++j than to argue with
them. (FWIW: K&R favored j++, and at least in the earlier
versions of his books, so did Stroustrup, so for older
programmers, who learned from the original masters, j++ often
seems more natural. Just because we've seen it more often,
however; not for any technical reason.)

--
James Kanze
 
Reply With Quote
 
Sprechen sie C++
Guest
Posts: n/a
 
      04-26-2011
http://www.contract-developer.tk/php...c.php?f=3&t=25

That is a short article on how to use iterators in place of a crude for loop

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
plain iterators and reverse iterators on vector subramanian100in@yahoo.com, India C++ 10 08-08-2009 08:28 AM
Why are "broken iterators" broken? Steven D'Aprano Python 8 09-28-2008 09:19 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Iterators and reverse iterators Marcin Kaliciński C++ 1 05-08-2005 09:58 AM



Advertisments