Topic: Ranged-for iteration of equal range of std::multimap
Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Tue, 25 Dec 2018 21:24:03 +1000
Raw View
--0000000000001ec0a7057dd6f4b6
Content-Type: text/plain; charset="UTF-8"
Given a std::multimap<K,V> m, how do you iterate over all the elements with
some key k0?
Am I right in believing that the cleanest way is:
auto er = m.equal_range(k0);
for (auto it = er.first; it != er.second; ++it) {
const auto& [k, v] = *it;
// do stuff with k,v
}
Or is there a better way?
What would we think about having a way to write something like:
for (const auto& [k,v] : m[k0]) {
// do stuff with k,v
}
ie std::multimap::operator[] would produce some sort of container view of
the .equal_range?
--
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/CAB%2B4KHKSbBK1K4Dx0n02Gjouy%2Bw5Y6ihmMys7ORPjNH6ujuOTA%40mail.gmail.com.
--0000000000001ec0a7057dd6f4b6
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Given a std::multimap<K,V> m, how do you iterate ove=
r all the elements with some key k0?<div><br></div><div>Am I right in belie=
ving that the cleanest way is:</div><div><br></div><div>=C2=A0 =C2=A0 auto =
er =3D m.equal_range(k0);</div><div>=C2=A0 =C2=A0 for (auto it =3D er.first=
; it !=3D er.second;=C2=A0++it) {</div><div>=C2=A0 =C2=A0 =C2=A0 const auto=
& [k, v] =3D *it;</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // do s=
tuff with k,v</div><div>=C2=A0 =C2=A0 }</div><div><br></div><div>Or is ther=
e a better way?</div><div><br></div><div>What would we think about having a=
way to write something like:</div><div><br></div><div>=C2=A0 =C2=A0for (co=
nst auto& [k,v] : m[k0]) {</div><div>=C2=A0 =C2=A0 =C2=A0 // do stuff w=
ith k,v</div><div>=C2=A0 =C2=A0}</div><div><br></div><div>ie std::multimap:=
:operator[] would produce some sort of container view of the .equal_range?<=
/div><div><br></div></div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHKSbBK1K4Dx0n02Gjouy%2Bw5Y6ih=
mMys7ORPjNH6ujuOTA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHKSbB=
K1K4Dx0n02Gjouy%2Bw5Y6ihmMys7ORPjNH6ujuOTA%40mail.gmail.com</a>.<br />
--0000000000001ec0a7057dd6f4b6--
.
Author: =?UTF-8?Q?=27Thomas_K=C3=B6ppe=27_via_ISO_C=2B=2B_Standard_=2D_Future_Proposals?= <std-proposals@isocpp.org>
Date: Tue, 25 Dec 2018 13:48:39 -0800 (PST)
Raw View
------=_Part_1485_1525192371.1545774519887
Content-Type: multipart/alternative;
boundary="----=_Part_1486_1593554154.1545774519888"
------=_Part_1486_1593554154.1545774519888
Content-Type: text/plain; charset="UTF-8"
On Tuesday, 25 December 2018 11:24:18 UTC, Andrew Tomazos wrote:
>
> Given a std::multimap<K,V> m, how do you iterate over all the elements
> with some key k0?
>
> Am I right in believing that the cleanest way is:
>
> auto er = m.equal_range(k0);
> for (auto it = er.first; it != er.second; ++it) {
> const auto& [k, v] = *it;
>
> // do stuff with k,v
> }
>
> Or is there a better way?
>
> What would we think about having a way to write something like:
>
> for (const auto& [k,v] : m[k0]) {
> // do stuff with k,v
> }
>
> ie std::multimap::operator[] would produce some sort of container view of
> the .equal_range?
>
It looks like all you need is a kind of "iterator range" adaptor that can
be constructed from a pair. The one in Boost may not be quite it, though
you could have your own library function that emits a boost::iterator_range
(or similar type) once and for all, and then just use:
* for (const auto& [k, v] : make_range(m.equal_range(key)**)**) { /* ...
*/ }*
That seems like a sufficiently accessible solution to a rare problem that
might be preferable to growing the already large interface of
std::{,unordered_}multi{map,set}. Not that it would be hard to grow the
interface, it just doesn't seem like that would buy us a massive
improvement in usability. The putative new interface would need to be
taught and understood (e.g. what does "m[key] = x;" mean?), whereas my
proposed approach uses entirely known and familiar patterns.
--
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/8568a673-2342-447d-9eeb-6cc335eeb942%40isocpp.org.
------=_Part_1486_1593554154.1545774519888
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Tuesday, 25 December 2018 11:24:18 UTC, Andrew Tomazos =
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">Given =
a std::multimap<K,V> m, how do you iterate over all the elements with=
some key k0?<div><br></div><div>Am I right in believing that the cleanest =
way is:</div><div><br></div><div>=C2=A0 =C2=A0 auto er =3D m.equal_range(k0=
);</div><div>=C2=A0 =C2=A0 for (auto it =3D er.first; it !=3D er.second;=C2=
=A0++it) {</div><div>=C2=A0 =C2=A0 =C2=A0 const auto& [k, v] =3D *it;</=
div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 // do stuff with k,v</div><div=
>=C2=A0 =C2=A0 }</div><div><br></div><div>Or is there a better way?</div><d=
iv><br></div><div>What would we think about having a way to write something=
like:</div><div><br></div><div>=C2=A0 =C2=A0for (const auto& [k,v] : m=
[k0]) {</div><div>=C2=A0 =C2=A0 =C2=A0 // do stuff with k,v</div><div>=C2=
=A0 =C2=A0}</div><div><br></div><div>ie std::multimap::operator[] would pro=
duce some sort of container view of the .equal_range?</div></div></blockquo=
te><div><br></div><div>It looks like all you need is a kind of "iterat=
or range" adaptor that can be constructed from a pair. The one in Boos=
t may not be quite it, though you could have your own library function that=
emits a boost::iterator_range (or similar type) once and for all, and then=
just use:</div><div><br></div><div><font face=3D"courier new, monospace"><=
b>=C2=A0 =C2=A0 for (const auto& [k, v] : <font color=3D"#38761d">make_=
range(</font>m.equal_range(key)</b></font><b style=3D"font-family: "co=
urier new", monospace;"><font color=3D"#38761d">)</font></b><font face=
=3D"courier new, monospace"><b>)=C2=A0{ /* ... */ }</b></font></div><div><b=
r></div><div>That seems like a sufficiently accessible solution to a rare p=
roblem that might be preferable to growing the already large interface of s=
td::{,unordered_}multi{map,set}. Not that it would be hard to grow the inte=
rface, it just doesn't seem like that would buy us a massive improvemen=
t in usability. The putative new interface would need to be taught and unde=
rstood (e.g. what does "m[key] =3D x;" mean?), whereas my propose=
d approach uses entirely known and familiar patterns.</div></div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/8568a673-2342-447d-9eeb-6cc335eeb942%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/8568a673-2342-447d-9eeb-6cc335eeb942=
%40isocpp.org</a>.<br />
------=_Part_1486_1593554154.1545774519888--
------=_Part_1485_1525192371.1545774519887--
.
Author: Barry Revzin <barry.revzin@gmail.com>
Date: Wed, 26 Dec 2018 12:14:04 -0800 (PST)
Raw View
------=_Part_1745_89713387.1545855244669
Content-Type: text/plain; charset="UTF-8"
If you change the spelling from
make_range(m.equal_range(key))
to
std::ranges::subrange(m.equal_range(key))
I believe that this will already work in C++20, there's a deduction guide for pair like. http://eel.is/c++draft/range.subrange
--
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/400a6075-19ca-4541-9e65-1153e1f7e79a%40isocpp.org.
------=_Part_1745_89713387.1545855244669--
.