Topic: Reverse inserter


Author: NDos Dannyu <ndospark320@naver.com>
Date: Sat, 5 Mar 2016 21:16:17 -0800 (PST)
Raw View
------=_Part_4018_2001660035.1457241377780
Content-Type: multipart/alternative;
 boundary="----=_Part_4019_579634652.1457241377780"

------=_Part_4019_579634652.1457241377780
Content-Type: text/plain; charset=UTF-8

std::inserter uses the plain iterator of the container, so it can't be used
to insert elements in reverse order.
std::insert_iterator can't be passed to std::make_reverse_iterator, because
it has void member types.
So I suggest adding reverse inserter, Like this:

*namespace std {*
*    template <class Container> class reverse_insert_iterator {*
*    private:*
*        typedef typename Container::iterator __i;*
*    protected:*
*        Container *container;*
*        __i iter;*
*    public:*
*        typedef Container container_type;*
*        typedef void value_type, difference_type, pointer, reference;*
*        typedef output_iterator_tag iterator_category;*
*        explicit reverse_insert_iterator(Container &c, __i i) noexcept :*
*            container(&c), iter(make_reverse_iterator(i)) {}*
*        reverse_insert_iterator &operator = (const typename
Container::value_type &value) {*
*            iter = container->insert(iter, value);*
            // Note that iter isn't increased here and thus simulates
reverse insertion.
*            return *this;*
*        }*
*        reverse_insert_iterator &operator = (typename
Container::value_type &&value) {*
*            iter = container->insert(iter, value);*
            // Ditto.
*            return *this;*
*        }*
*        reverse_insert_iterator &operator * ()  noexcept {*
*            return *this;*
*        }*
*        reverse_insert_iterator &operator ++ () noexcept {*
*            return *this;*
*        }*
*        reverse_insert_iterator &operator ++ (int) noexcept {*
*            return *this;*
*        }*
*    };*
*    template <class Container> *
*    reverse_insert_iterator<Container> reverse_inserter(Container &c,
typename Container::iterator i) noexcept {*
*        return reverse_insert_iterator<Container>(c, i);*
*    }*
*}*

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/bb3d07fc-c16d-4aee-99ba-769751c06aa5%40isocpp.org.

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

<div dir=3D"ltr"><div>std::inserter uses the plain iterator of the containe=
r, so it can&#39;t be used to insert elements in reverse order.</div><div>s=
td::insert_iterator can&#39;t be passed to std::make_reverse_iterator, beca=
use it has void member types.</div><div>So I suggest adding reverse inserte=
r, Like this:</div><div><br></div><div><b>namespace std {</b></div><div><b>=
=C2=A0=C2=A0=C2=A0 template &lt;class Container&gt; class reverse_insert_it=
erator {</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0private:</b></div><div><b=
>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 typedef typename Container::ite=
rator __i;</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0protected:</b></div><di=
v><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Container *container;</b></=
div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 __i iter;</b></div><=
div><b>=C2=A0=C2=A0=C2=A0 public:</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 typedef Container container_type;</b></div><div><b>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0typedef void value_type, diffe=
rence_type, pointer, reference;</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 typedef output_iterator_tag iterator_category;</b></div>=
<div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 explicit reverse_insert_=
iterator(Container &amp;c, __i i) noexcept :</b></div><div><b>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 container(&amp;c), i=
ter(make_reverse_iterator(i)) {}</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 reverse_insert_iterator &amp;operator =3D (const typenam=
e Container::value_type &amp;value) {</b></div><div><b>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 iter =3D container-&gt;ins=
ert(iter, value);</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 </b>// Note that iter isn&#39;t increased here =
and thus simulates reverse insertion.</div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;</b></div><div><b>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }</b><div><b>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 reverse_insert_iterator &amp;operator =3D (typenam=
e Container::value_type &amp;&amp;value) {</b></div><div><b>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 iter =3D container-&gt;=
insert(iter, value);</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 </b>// Ditto.</div><div><b>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;</b></div>=
<div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }</b></div><div><b>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0reverse_insert_iterator &amp;o=
perator * ()=C2=A0 noexcept {</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;</b></div><div><b>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}</b><div><b>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0reverse_insert_iterator &amp;operator=C2=
=A0++ () noexcept {</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return *this;</b></div><div><b>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}</b><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0reverse_insert_iterator &amp;operator=C2=A0++ (int)=
 noexcept {</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 return *this;</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0}</b></div></div></div><div><b>=C2=A0=C2=A0=C2=
=A0 };</b></div><div><b>=C2=A0=C2=A0=C2=A0 template &lt;class Container&gt;=
 </b></div><div><b>=C2=A0=C2=A0=C2=A0 reverse_insert_iterator&lt;Container&=
gt; reverse_inserter(Container &amp;c, typename Container::iterator i) noex=
cept {</b></div><div><b>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return r=
everse_insert_iterator&lt;Container&gt;(c, i);</b></div><div><b>=C2=A0=C2=
=A0=C2=A0 }</b></div><div><b>}</b></div></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/bb3d07fc-c16d-4aee-99ba-769751c06aa5%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/bb3d07fc-c16d-4aee-99ba-769751c06aa5=
%40isocpp.org</a>.<br />

------=_Part_4019_579634652.1457241377780--
------=_Part_4018_2001660035.1457241377780--

.


Author: Arthur O'Dwyer <arthur.j.odwyer@gmail.com>
Date: Sun, 6 Mar 2016 23:06:58 -0800 (PST)
Raw View
------=_Part_5051_1633640209.1457334418761
Content-Type: multipart/alternative;
 boundary="----=_Part_5052_1913435158.1457334418761"

------=_Part_5052_1913435158.1457334418761
Content-Type: text/plain; charset=UTF-8

On Saturday, March 5, 2016 at 9:16:17 PM UTC-8, NDos Dannyu wrote:
>
> std::inserter uses the plain iterator of the container, so it can't be
> used to insert elements in reverse order.
>

I imagine that most of the use-cases for such a thing could be handled with
std::front_inserter.
http://en.cppreference.com/w/cpp/iterator/front_insert_iterator
http://en.cppreference.com/w/cpp/iterator/front_inserter

IIUC, you're looking specifically for a way to
- insert elements from the range B...E
- into an existing (possibly non-empty) *container*
- not at the front, but rather somewhere in the *middle* (otherwise
front_inserter would fit the bill)
- where the inserted elements must wind up in *reverse order*
- where for some reason inserting the elements "in non-reversed order, from
the range make_reverse_iterator(E)...make_reverse_iterator(B)" would be
unacceptable

and you really want to use a *standard algorithm* for this instead of
writing a for loop.

I suspect that it wouldn't be worth adding to the STL's zoo of weird
iterator types, given that Ranges are going to arrive soon. How does Ranges
handle insertion?

my $.02,
Arthur

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/81f682a3-c4ce-42c3-9cb1-4e1ae06cacd0%40isocpp.org.

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

<div dir=3D"ltr">On Saturday, March 5, 2016 at 9:16:17 PM UTC-8, NDos Danny=
u wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
..8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div>=
std::inserter uses the plain iterator of the container, so it can&#39;t be =
used to insert elements in reverse order.</div></div></blockquote><div><br>=
</div><div>I imagine that most of the use-cases for such a thing could be h=
andled with=C2=A0<font face=3D"courier new, monospace">std::front_inserter<=
/font>.</div><div><a href=3D"http://en.cppreference.com/w/cpp/iterator/fron=
t_insert_iterator">http://en.cppreference.com/w/cpp/iterator/front_insert_i=
terator<br></a></div><div><a href=3D"http://en.cppreference.com/w/cpp/itera=
tor/front_inserter">http://en.cppreference.com/w/cpp/iterator/front_inserte=
r<br></a></div><div><br></div><div>IIUC, you&#39;re looking specifically fo=
r a way to</div><div>- insert elements from the range B...E</div><div>- int=
o an existing (possibly non-empty) <i>container</i></div><div>- not at the =
front, but rather somewhere in the <i>middle</i> (otherwise <font face=3D"c=
ourier new, monospace">front_inserter</font> would fit the bill)</div><div>=
- where the inserted elements must wind up in <i>reverse order</i></div><di=
v>- where for some reason inserting the elements &quot;in non-reversed orde=
r, from the range make_reverse_iterator(E)...make_reverse_iterator(B)&quot;=
 would be unacceptable</div><div><br></div><div>and you really want to use =
a <i>standard algorithm</i> for this instead of writing a <font face=3D"cou=
rier new, monospace">for</font> loop.</div><div><br></div><div>I suspect th=
at it wouldn&#39;t be worth adding to the STL&#39;s zoo of weird iterator t=
ypes, given that Ranges are going to arrive soon. How does Ranges handle in=
sertion?</div><div><br></div><div>my $.02,</div><div>Arthur</div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/81f682a3-c4ce-42c3-9cb1-4e1ae06cacd0%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/81f682a3-c4ce-42c3-9cb1-4e1ae06cacd0=
%40isocpp.org</a>.<br />

------=_Part_5052_1913435158.1457334418761--
------=_Part_5051_1633640209.1457334418761--

.