Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-11-09 16:10:45


"John Torjo" <john.lists_at_[hidden]> wrote in message
news:41909351.4030100_at_torjo.com...

example 1

> >>It makes dealing with containers/STL algorithms simpler by using ranges
> >>(as opposed to iterators).
> >
> >
> >>for ( crange<some_array_type> r(some_array); r; ++r)
> >> do_something( *r, some_data);
> >
> >
> > How is this simpler or more transparent than
> >
> > for(some_array::iterator i=some_array.begin(); i != some_array.end();
++i)
> > do_something( *i, some_data);
> >
>
> more compact, and not error prone.

I see only the smallest difference here.

> Also, very useful in combination with algorithms that return ranges.

I'll address this below

example 2

> >>Also, the library provides wrappers for all STL algorithms (for the
> >>purpose of STL algorithms, all containers are ranges as well):
> >>
> >>typedef std::vector<std::string> word_array;
> >>word_array v;
> >>// STL version
> >>std::copy( v.begin(), v.end(), print);
> >>// rng:: version
> >>rng::copy( v, print);
> >
> >
> > I don't see this as adding any value or conceptual transparency.
>
> Not sure I understand what you mean.

This is really an extension of my observation above (marked example 1). My
view is that
the concept of ranges doesn't add enough to the concept of iterators to make
it worth
adding another layer to remember.

> > std::vector<int> result;
> > std::string str = "I'm a cool programmer, really cool that is";
> > std::string find = "cool";
> >
> > std::string::const_iterator r = str.begin();
> > while(r = stl::search(r, find.begin(), find.end()))
> > result.push_back ( r.begin() - str.begin() );
> >
> > which to my mind isn't any different or more complex.
>
> in fact, you forgot to compare r to str.end()

correct

while(str.end != (r = stl::search(r, find.begin(), find.end())))

again, I don't see that using ranges adds anything

> >>
> >>// take all employees from Romania, and print their names
> >>rng::copy( transformed( filtered(empls,from_ro), get_empl_name),
> >> std::ostream_iterator<std::string>(std::cout," "));
> >>
> >
> >
> > I don't see how this is better than using iterator adaptors for
composition.
>

Attached is the equivalent code using iterators.

It includes code to add templeted constructors to iterators and to make
functors for this particular example. Otherwise its very similar in
functionality. And that is my point. I don't see that ranges are anything
more than a very thin layer over iterators. Admitidly, iterators could
perhaps use some embellishment (perhaps templated constructors) or other
things to make them easier to use, but I don't think a whole now layer/view
is warrented. I would prefer to see such efforts invested in making
iterators easier to use without hidind their essential character.

Robert Ramey

begin 666 test_zmisc.cpp
M(VEN8VQU9&4@/'9E8W1O<CX-"B-I;F-L=61E(#QS=')I;F<^#0HC:6YC;'5D
M92 \86QG;W)I=&AM/@T*(VEN8VQU9&4@/&9U;F-T:6]N86P^#0HC:6YC;'5D
M92 \:6]S=')E86T^#0HC:6YC;'5D92 \8F]O<W0O:71E<F%T;W(O9FEL=&5R
M7VET97)A=&]R+FAP<#X-"B-I;F-L=61E(#QB;V]S="]I=&5R871O<B]T<F%N
M<V9O<FU?:71E<F%T;W(N:'!P/@T*#0IS=')U8W0_at_96UP;&]Y964@>PT*(" @
M('-T9#HZ<W1R:6YG(&YA;64[#0H@(" @<W1D.CIS=')I;F<@8V]U;G1R>3L-
M"GT[#0H-"F)O;VP_at_9G)O;5]R;RAC;VYS="!E;7!L;WEE92 F(&4I('L@<F5T
M=7)N(&4N8V]U;G1R>2 ]/2 B4F]M86YI82([('T-"F-O;G-T('-T9#HZ<W1R
M:6YG("8_at_9V5T7V5M<&Q?;F%M92AC;VYS="!E;7!L;WEE92 F92D@>R!R971U
M<FX_at_92YN86UE.R!]#0H-"B\O(&%M96YD(&)Y('=A>2!O9B!D97)I=F%T:6]N
M(&)O;W-T('1R86YS9F]R;2!A;F0_at_9FEL=&5R(&ET97)A=&]R<R -"B\O('1O
M(&EN8VQU9&4@=&5M<&QA=&5D(&-O;G-T<G5C=&EO;BX@(%1H:7,@<V%V97,@
M;&]T<R!O9B!T>7!I;F<N#0HO+R!097)H87!S('-O;65D87D@=&5M<&QA=&5D
M(&-O;G-T<G5C=&]R<R!M:6=H="!B92!A9&1E9"!T;R -"B\O(&ET97)A=&]R
M(&%D87!T;W)S("T@=&AE<F5B>2!M86MI;F<@<W5C:"!A;65N9&UE;G1S('5N
M;F5C97-S87)Y#0IN86UE<W!A8V4_at_86UE;F1E9"![#0H-"G1E;7!L871E(#P-
M"B @("!C;&%S<R!5;F%R>49U;F-T:6]N+ T*(" @(&-L87-S($ET97)A=&]R
M+" -"B @("!C;&%S<R!2969E<F5N8V4@/2!B;V]S=#HZ=7-E7V1E9F%U;'0L
M( T*(" @(&-L87-S(%9A;'5E(#T_at_8F]O<W0Z.G5S95]D969A=6QT#0H^#0IC
M;&%S<R!T<F%N<V9O<FU?:71E<F%T;W(@.B -"B @("!P=6)L:6,@8F]O<W0Z
M.G1R86YS9F]R;5]I=&5R871O<CP-"B @(" @(" @56YA<GE&=6YC=&EO;BP-
M"B @(" @(" @271E<F%T;W(L( T*(" @(" @("!2969E<F5N8V4L( T*(" @
M(" @("!686QU90T*(" @(#X-"GL-"G!U8FQI8SH-"B @("!T96UP;&%T93QC
M;&%S<R!4/@T*(" @('1R86YS9F]R;5]I=&5R871O<BAC;VYS="!4("8@<W1A
M<G0I(#H-"B @(" @(" @8F]O<W0Z.G1R86YS9F]R;5]I=&5R871O<CP-"B @
M(" @(" @(" @(%5N87)Y1G5N8W1I;VXL#0H@(" @(" @(" @("!)=&5R871O
M<BP@#0H@(" @(" @(" @("!2969E<F5N8V4L( T*(" @(" @(" @(" @5F%L
M=64-"B @(" @(" @/BA)=&5R871O<BAS=&%R="DL(%5N87)Y1G5N8W1I;VXH
M*2D-"B @("![?0T*?3L-"@T*=&5M<&QA=&4@/&-L87-S(%!R961I8V%T92P@
M8VQA<W,@271E<F%T;W(^#0IC;&%S<R!F:6QT97)?:71E<F%T;W(@(#H-"B @
M("!P=6)L:6,@8F]O<W0Z.F9I;'1E<E]I=&5R871O<CP-"B @(" @(" @4')E
M9&EC871E+ T*(" @(" @("!)=&5R871O<@T*(" @(#X-"GL-"G!U8FQI8SH-
M"B @("!T96UP;&%T93QC;&%S<R!4/@T*(" @(&9I;'1E<E]I=&5R871O<BAC
M;VYS="!4("8@<W1A<G0I(#H-"B @(" @(" @8F]O<W0Z.F9I;'1E<E]I=&5R
M871O<CP-"B @(" @(" @(" @(%!R961I8V%T92P-"B @(" @(" @(" @($ET
M97)A=&]R#0H@(" @(" @(#XH4')E9&EC871E*"DL($ET97)A=&]R*'-T87)T
M*2D-"B @("![?0T*?3L-"@T*?2 O+R!N86UE<W!A8V4_at_86UE;F1E9 T*#0IS
M=')U8W0_at_9V5T7V5M<&Q?;F%M95]F(#H@#0H@(" @<'5B;&EC('-T9#HZ=6YA
M<GE?9G5N8W1I;VX\8V]N<W0_at_96UP;&]Y964@)BP_at_8V]N<W0@<W1D.CIS=')I
M;F<@)CX-"GL-"B @("!C;VYS="!S=&0Z.G-T<FEN9R F(&]P97)A=&]R*"DH
M8V]N<W0_at_96UP;&]Y964@)F4I(&-O;G-T('L-"B @(" @(" @<F5T=7)N(&=E
M=%]E;7!L7VYA;64H92D[#0H@(" @?0T*?3L-"@T*<W1R=6-T(&9R;VU?<F]?
M9B Z#0H@(" @<'5B;&EC('-T9#HZ=6YA<GE?9G5N8W1I;VX\8V]N<W0_at_96UP
M;&]Y964@)BP_at_8V]N<W0@<W1D.CIS=')I;F<@)CX-"GL-"B @("!B;V]L(&]P
M97)A=&]R*"DH8V]N<W0_at_96UP;&]Y964@)F4I(&-O;G-T('L-"B @(" @(" @
M<F5T=7)N(&9R;VU?<F\H92D[#0H@(" @?0T*?3L-"@T*#0II;G0@;6%I;B_at_I
M>PT*(" @('1Y<&5D968@<W1D.CIV96-T;W(\96UP;&]Y964^(&%R<F%Y.PT*
M(" @(&%R<F%Y(&5M<&QS.PT*(" @( T*(" @('1Y<&5D968_at_86UE;F1E9#HZ
M=')A;G-F;W)M7VET97)A=&]R/ T*(" @(" @("!G971?96UP;%]N86UE7V8L
M#0H@(" @(" @(&%M96YD960Z.F9I;'1E<E]I=&5R871O<CP-"B @(" @(" @
M(" @(&9R;VU?<F]?9BP-"B @(" @(" @(" @('-T9#HZ=F5C=&]R/&5M<&QO
M>65E/CHZ8V]N<W1?:71E<F%T;W(-"B @(" @(" @/@T*(" @(#X@<F]M86YI
M86Y?;F%M97-?=#L-"B @(" @( T*(" @('-T9#HZ8V]P>2_at_-"B @(" @(" @
M<F]M86YI86Y?;F%M97-?="AE;7!L<RYB96=I;B_at_I*2P-"B @(" @(" @<F]M
M86YI86Y?;F%M97-?="AE;7!L<RYE;F0H*2DL#0H@(" @(" @('-T9#HZ;W-T
M<F5A;5]I=&5R871O<CQS=&0Z.G-T<FEN9SXH<W1D.CIC;W5T+"(@(BD-"B @
M(" I.R @( T*?0T*(" @("\O('1A:V4_at_86QL(&5M<&QO>65E<R!F<F]M(%)O
M;6%N:6$L(&%N9"!P<FEN="!T:&5I<B!N86UE<PT*+R\@(" @<W1D.CIC;W!Y
M*"!T<F%N<V9O<FUE9"@@9FEL=&5R960H96UP;',L9G)O;5]R;RDL(&=E=%]E
M;7!L7VYA;64I+ T*+R\@(" @(" @('-T9#HZ;W-T<F5A;5]I=&5R871O<CQS
@=&0Z.G-T<FEN9SXH<W1D.CIC;W5T+"(@(BDI.PT*#0H`
`
end


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk