Topic: Using the reverse iterator of standard containers


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Tue, 21 Jul 2015 06:38:49 -0700 (PDT)
Raw View
------=_Part_75_1090841688.1437485930046
Content-Type: multipart/alternative;
 boundary="----=_Part_76_1505598536.1437485930046"

------=_Part_76_1505598536.1437485930046
Content-Type: text/plain; charset=UTF-8

Here is a demonstration of the idea..

Consider the following program

#include <iostream>
#include <vector>

int main()
{
    const int N = 10;
    std::vector<int> v;
    v.reserve( N );

    v.push_back( 0 );

    auto it = v.end();

    for ( int i = 1; i < N; i++ ) it = v.insert( it, i );

    for ( auto x : v ) std::cout << x << ' ';
    std::cout << std::endl;
}

Its output is

0 9 8 7 6 5 4 3 2 1

If the ireverse iterator  could be used with member function insert we can
write

#include <iostream>
#include <vector>

int main()
{
    const int N = 10;
    std::vector<int> v;
    v.reserve( N );

    v.push_back( 0 );

    auto it = v.rbegin();

    for ( int i = 1; i < N; i++ ) it = v.insert( it, i );

    for ( auto x : v ) std::cout << x << ' ';
    std::cout << std::endl;
}

In this case the output would look like

0 1 2 3 4 5 6 7 8 9

Or if we have

#include <iostream>
#include <vector>

int main()
{
    const int N = 10;
    std::vector<int> v;
    v.reserve( N );

    v.push_back( 0 );

    auto it = v.begin();

    for ( int i = 1; i < N; i++ ) it = v.insert( it, i );

    for ( auto x : v ) std::cout << x << ' ';
    std::cout << std::endl;
}
then the output is

9 8 7 6 5 4 3 2 1 0

If we could replace the iterator with the reverse_iterator like this

#include <iostream>
#include <vector>

int main()
{
    const int N = 10;
    std::vector<int> v;
    v.reserve( N );

    v.push_back( 0 );

    auto it = v.rend();

    for ( int i = 1; i < N; i++ ) it = v.insert( it, i );

    for ( auto x : v ) std::cout << x << ' ';
    std::cout << std::endl;
}

then the output would be

0 1 2 3 4 5 6 7 8 9

On Tuesday, July 21, 2015 at 3:44:31 PM UTC+3, Vlad from Moscow wrote:

> The idea is to overload some member functions of standard containers as
> for example insert and erase such a way that they could use the reverse
> iterator.of the used container
>
> For example one member function insert of std::vector is declared like
>
> iterator insert(const_iterator position, const T& x);
>
> Why not to introduce member function insert that would be declared like
>
> reverse_iterator insert(const_reverse_iterator position, const T& x);
>
> In my opinion it would be useful.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_76_1505598536.1437485930046
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Here is a=C2=A0demonstration of the idea..</div><div>=
<br></div><div>Consider the following program</div><div><br></div><div>#inc=
lude &lt;iostream&gt;<br>#include &lt;vector&gt;</div><div><br></div><div>i=
nt main() <br>{<br>=C2=A0=C2=A0=C2=A0 const int N =3D 10;<br>=C2=A0=C2=A0=
=C2=A0 std::vector&lt;int&gt; v;<br>=C2=A0=C2=A0=C2=A0 v.reserve( N );</div=
><div><br></div><div>=C2=A0=C2=A0=C2=A0 v.push_back( 0 );<br>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 auto it =3D v.en=
d();<br>=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( int i =3D 1; i=
 &lt; N; i++ ) it =3D v.insert( it, i );<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( auto x : v ) std::cout &lt;&=
lt; x &lt;&lt; &#39; &#39;;<br>=C2=A0=C2=A0=C2=A0 std::cout &lt;&lt; std::e=
ndl;<br>}<br></div><div><br></div><div>Its output is</div><div><br></div><d=
iv>0 9 8 7 6 5 4 3 2 1</div><div><br></div><div>If=C2=A0the ireverse iterat=
or=C2=A0=C2=A0could be used with member function insert we can write</div><=
div><br></div><div><div>#include &lt;iostream&gt;<br>#include &lt;vector&gt=
;</div><div><br></div><div>int main() <br>{<br>=C2=A0=C2=A0=C2=A0 const int=
 N =3D 10;<br>=C2=A0=C2=A0=C2=A0 std::vector&lt;int&gt; v;<br>=C2=A0=C2=A0=
=C2=A0 v.reserve( N );</div><div><br></div><div>=C2=A0=C2=A0=C2=A0 v.push_b=
ack( 0 );<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=
=A0=C2=A0 auto it =3D v.rbegin();<br>=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=
=A0=C2=A0 for ( int i =3D 1; i &lt; N; i++ ) it =3D v.insert( it, i );<br>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for =
( auto x : v ) std::cout &lt;&lt; x &lt;&lt; &#39; &#39;;<br>=C2=A0=C2=A0=
=C2=A0 std::cout &lt;&lt; std::endl;<br>}<br></div><div>=C2=A0<br>In this c=
ase the output would look like</div><div><br></div><div>0 1 2 3 4 5 6 7 8 9=
</div><div><br></div><div>Or if we have</div><div><br></div><div>#include &=
lt;iostream&gt;<br>#include &lt;vector&gt;</div><div><br></div><div>int mai=
n() <br>{<br>=C2=A0=C2=A0=C2=A0 const int N =3D 10;<br>=C2=A0=C2=A0=C2=A0 s=
td::vector&lt;int&gt; v;<br>=C2=A0=C2=A0=C2=A0 v.reserve( N );</div><div><b=
r></div><div>=C2=A0=C2=A0=C2=A0 v.push_back( 0 );<br>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 auto it =3D v.begin();<br=
>=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( int i =3D 1; i &lt; N=
; i++ ) it =3D v.insert( it, i );<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( auto x : v ) std::cout &lt;&lt; x =
&lt;&lt; &#39; &#39;;<br>=C2=A0=C2=A0=C2=A0 std::cout &lt;&lt; std::endl;<b=
r>}<br></div></div><div>then the output is</div><div><br></div><div>9 8 7 6=
 5 4 3 2 1 0</div><div><br></div><div>If we could replace the iterator with=
 the reverse_iterator like this</div><div><br></div><div><div>#include &lt;=
iostream&gt;<br>#include &lt;vector&gt;</div><div><br></div><div>int main()=
 <br>{<br>=C2=A0=C2=A0=C2=A0 const int N =3D 10;<br>=C2=A0=C2=A0=C2=A0 std:=
:vector&lt;int&gt; v;<br>=C2=A0=C2=A0=C2=A0 v.reserve( N );</div><div><br><=
/div><div>=C2=A0=C2=A0=C2=A0 v.push_back( 0 );<br>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 auto it =3D v.rend();<br>=C2=
=A0=C2=A0=C2=A0=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( int i =3D 1; i &lt; N; i+=
+ ) it =3D v.insert( it, i );<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0<br>=C2=A0=C2=A0=C2=A0 for ( auto x : v ) std::cout &lt;&lt; x &lt;&l=
t; &#39; &#39;;<br>=C2=A0=C2=A0=C2=A0 std::cout &lt;&lt; std::endl;<br>}<br=
></div></div><div><br></div><div>then the output would be</div><div><br></d=
iv><div>0 1 2 3 4 5 6 7 8 9</div><div><br>On Tuesday, July 21, 2015 at 3:44=
:31 PM UTC+3, Vlad from Moscow wrote:</div><blockquote class=3D"gmail_quote=
" style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color:=
 rgb(204, 204, 204); border-left-width: 1px; border-left-style: solid;"><di=
v dir=3D"ltr"><div>The idea is to overload some member functions of standar=
d containers as for example insert=C2=A0and erase such a way that they coul=
d=C2=A0use the reverse iterator.of the used container</div><div><br></div><=
div>For example one member function insert of std::vector is declared like=
=C2=A0</div><div><br></div><div>iterator insert(const_iterator position, co=
nst T&amp; x);</div><div><br></div><div>Why not to introduce member functio=
n insert that would be declared like</div><div><br></div><div><div>reverse_=
iterator insert(const_reverse_iterator position, const T&amp; x);</div><div=
><br></div><div>In my opinion it would be useful.</div></div><font face=3D"=
LMMono9-Regular" size=3D"1"><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div><f=
ont face=3D"LMMono9-Regular" size=3D"1"><div><br></div><p align=3D"left"><b=
r></p></font><p align=3D"left"><br></p></font><p align=3D"left"><br></p><p =
align=3D"left"><br></p><p align=3D"left"><br></p></div></blockquote></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_76_1505598536.1437485930046--
------=_Part_75_1090841688.1437485930046--

.