Topic: std::map::insert_or_assign with range
Author: Tomasz <tj.jaskolski@gmail.com>
Date: Sat, 7 Mar 2015 06:19:47 -0800 (PST)
Raw View
------=_Part_1689_2062369991.1425737987288
Content-Type: multipart/alternative;
boundary="----=_Part_1690_1866376157.1425737987288"
------=_Part_1690_1866376157.1425737987288
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hi Everyone,
I would like to propose extending this #N4279=20
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4279> proposal=20
with one std::map::insert_or_assign overload. Consider code like follow:
std::map<int, int> foo { {1, 2}, {2, 3}, {3, 4} };
std::map<int, int> bar { {1, 5}, {4, 6}, {2, 7} };
=20
for (const auto& v : bar)
foo[v.first] =3D v.second;
As you can see we are going to insert or update values from bar to foo. To=
=20
achieve that goal we may write it down in other ways, for instance:
std::for_each(std::begin(bar), std::end(bar), [&foo](const auto& v) {=20
foo[v.first] =3D v.second; });
or (in assumption that bar is non const):
bar.insert(std::begin(foo), std::end(foo));
foo.swap(bar);
In my opinion it would be nice if std::map::insert_or_assign may take range=
=20
and apply insert or update (assign) operation for each element from that=20
range to destination map.
Similar overload is already defined in std::map::insert operation:
template <template InputIt>
void insert(InputIt first, InputIt last);
What do you think?
Best regards,
Tomasz Jask=C3=B3lski (Satirev)
--=20
---=20
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 e=
mail 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-proposa=
ls/.
------=_Part_1690_1866376157.1425737987288
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hi Everyone,<br><br>I would like to propose extending this=
<a href=3D"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4279">=
#N4279</a> proposal with one std::map::insert_or_assign overload. Cons=
ider code like follow:<br><br><div class=3D"prettyprint" style=3D"border: 1=
px solid rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(2=
50, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
div class=3D"subprettyprint">std::map<int, int> foo { {1, 2}, {2, 3},=
{3, 4} };</div><div class=3D"subprettyprint">std::map<int, int> bar =
{ {1, 5}, {4, 6}, {2, 7} };</div><div class=3D"subprettyprint"><span class=
=3D"Apple-tab-span" style=3D"white-space:pre"> </span> <s=
pan class=3D"Apple-tab-span" style=3D"font-family: Arial, Helvetica, sans-s=
erif; white-space: pre;"> </span></div><div class=3D"subprettyprint">for (c=
onst auto& v : bar)</div><div class=3D"subprettyprint"> foo[v.fir=
st] =3D v.second;</div></div></code></div><br>As you can see we are going t=
o insert or update values from bar to foo. To achieve that goal we may writ=
e it down in other ways, for instance:<br><br><div class=3D"prettyprint" st=
yle=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgro=
und-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"s=
ubprettyprint"><div class=3D"subprettyprint"><span style=3D"font-family: Ar=
ial, Helvetica, sans-serif;">std::for_each(std::begin(bar), std::end(bar), =
[&foo](const auto& v) { foo[v.first] =3D v.second; });</span></div>=
</div></code></div><br>or (in assumption that bar is non const):<br><br><di=
v class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word=
-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=3D"pr=
ettyprint"><div class=3D"subprettyprint"><div class=3D"subprettyprint"><spa=
n style=3D"font-family: Arial, Helvetica, sans-serif;">bar.insert(std::begi=
n(foo), std::end(foo));</span></div><div class=3D"subprettyprint">foo.swap(=
bar);</div></div></code></div><br>In my opinion it would be nice if std::ma=
p::insert_or_assign may take range and apply insert or update (assign) oper=
ation for each element from that range to destination map.<br>Similar overl=
oad is already defined in std::map::insert operation:<br><br><div class=3D"=
prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: brea=
k-word; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint">=
<div class=3D"subprettyprint"><span class=3D"kw1" style=3D"line-height: 14.=
0799999237061px; color: rgb(0, 0, 221); font-family: DejaVuSansMono, 'DejaV=
u Sans Mono', courier, monospace; font-size: 12.8000001907349px; white-spac=
e: nowrap; background-color: rgb(255, 255, 255);">template </span><spa=
n class=3D"sy1" style=3D"line-height: 14.0799999237061px; color: rgb(0, 0, =
128); font-family: DejaVuSansMono, 'DejaVu Sans Mono', courier, monospace; =
font-size: 12.8000001907349px; white-space: nowrap; background-color: rgb(2=
55, 255, 255);"><</span><span class=3D"sy1" style=3D"line-height: 14.079=
9999237061px; font-family: DejaVuSansMono, 'DejaVu Sans Mono', courier, mon=
ospace; font-size: 12.8000001907349px; white-space: nowrap; background-colo=
r: rgb(255, 255, 255);"><font color=3D"#0000dd">template</font></span><span=
style=3D"color: rgb(0, 0, 0); font-family: DejaVuSansMono, 'DejaVu Sans Mo=
no', courier, monospace; font-size: 12.8000001907349px; line-height: 14.079=
9999237061px; white-space: nowrap; background-color: rgb(255, 255, 255);">&=
nbsp;InputIt</span><span class=3D"sy1" style=3D"line-height: 14.07999992370=
61px; color: rgb(0, 0, 128); font-family: DejaVuSansMono, 'DejaVu Sans Mono=
', courier, monospace; font-size: 12.8000001907349px; white-space: nowrap; =
background-color: rgb(255, 255, 255);">></span><br style=3D"color: rgb(0=
, 0, 0); font-family: DejaVuSansMono, 'DejaVu Sans Mono', courier, monospac=
e; font-size: 12.8000001907349px; line-height: 14.0799999237061px; white-sp=
ace: nowrap; background-color: rgb(255, 255, 255);"><span class=3D"kw4" sty=
le=3D"line-height: 14.0799999237061px; color: rgb(0, 0, 255); font-family: =
DejaVuSansMono, 'DejaVu Sans Mono', courier, monospace; font-size: 12.80000=
01907349px; white-space: nowrap; background-color: rgb(255, 255, 255);">voi=
d</span><span style=3D"color: rgb(0, 0, 0); font-family: DejaVuSansMono, 'D=
ejaVu Sans Mono', courier, monospace; font-size: 12.8000001907349px; line-h=
eight: 14.0799999237061px; white-space: nowrap; background-color: rgb(255, =
255, 255);"> insert</span><span class=3D"br0" style=3D"line-height: 14=
..0799999237061px; color: rgb(0, 128, 0); font-family: DejaVuSansMono, 'Deja=
Vu Sans Mono', courier, monospace; font-size: 12.8000001907349px; white-spa=
ce: nowrap; background-color: rgb(255, 255, 255);">(</span><span style=3D"c=
olor: rgb(0, 0, 0); font-family: DejaVuSansMono, 'DejaVu Sans Mono', courie=
r, monospace; font-size: 12.8000001907349px; line-height: 14.0799999237061p=
x; white-space: nowrap; background-color: rgb(255, 255, 255);">InputIt firs=
t, InputIt last</span><span class=3D"br0" style=3D"line-height: 14.07999992=
37061px; color: rgb(0, 128, 0); font-family: DejaVuSansMono, 'DejaVu Sans M=
ono', courier, monospace; font-size: 12.8000001907349px; white-space: nowra=
p; background-color: rgb(255, 255, 255);">)</span><span class=3D"sy4" style=
=3D"line-height: 14.0799999237061px; color: rgb(0, 128, 128); font-family: =
DejaVuSansMono, 'DejaVu Sans Mono', courier, monospace; font-size: 12.80000=
01907349px; white-space: nowrap; background-color: rgb(255, 255, 255);">;</=
span><br></div></code></div><br>What do you think?<br><br>Best regards,<br>=
Tomasz Jask=C3=B3lski (Satirev)</div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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_1690_1866376157.1425737987288--
------=_Part_1689_2062369991.1425737987288--
.