ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
[boost] Suggestion regarding next() and prior()
by Walker, Daniel other posts by this author
Nov 25 2003 9:14PM messages near this date
RE: [boost] Re: safe_bool alternative? | [boost] Re: Re: Suggestion regarding next() and prior()
Hello,

I'm new to the list, but I was just thing about the utilities next() and
prior(), and I have a suggestion. I think it would be very useful to
have a next_n() and prior_n() to simulate operators + and - for non
random access iterators. Often, I find myself in the situation where I
know the length of a sequence, but don't have the last iterator. For
example, I'd like to process all subsequences of length three. This is
fairly simple with random access iterators ...

    for(RandomAccessIterator i =3D first; i !=3D last - 2; ++i)
    {
        RandomAccessIterator my_first =3D i, my_last =3D i + 3;
        // ... do something
    }

But with bidirectional iterators ...

    BidirectionalIterator tmp_last =3D last;
    std::advance(tmp_last, -2);
    for(BidirectionalIterator i =3D first; i !=3D tmp_last; ++i)
    {
        BidirectionalIterator my_first =3D i, my_last =3D i;
        std::advance(my_last, 3);
        // ... do something
    }

What I'd like to be able to say is something like ...

    for(BidirectionalIterator i =3D first; i !=3D boost::prior_n(last, =
2);
++i)
    {
        BidirectionalIterator my_first =3D i, my_last =3D =
boost::next_n(i,
3);
        // ... do something
    }

The implementation would be straightforward.

template <class T, class Size> 
inline T next_n(T x, Size n)
{
    std::advance(x, n);
    return x;
}

template <class T, class Size> 
inline T prior_n(T x, Size n)
{
    std::advance(x, -n);
    return x;
}

Is there another way that I'm not aware of that would give me a similar
effect? If not, is there any interest in next_n() and prior_n()?

Thanks!
_________________________________
Daniel Walker, Software Engineer
Bowne Global Solutions

Office	5095 Murphy Canyon Road
	San Diego, CA 92101 USA
Phone 	+1 858 737 5247
Mobile	+1 619 251 4068
Fax	+1 858 737 5297
daniel.walker@[...].com
www.bowneglobal.com
Attachments:
unknown1

Thread:
Walker, Daniel
Fernando Cacciola
Rob Stewart
David Abrahams
David Abrahams
John Torjo
Fernando Cacciola

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved