Topic: [std-proposals] Add value function for associa
Author: Andrey Matveyakin <a.matveyakin@gmail.com>
Date: Mon, 27 Jan 2014 14:30:34 -0800 (PST)
Raw View
------=_Part_2283_14899533.1390861834408
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
I propose to add a function called =E2=80=9Cvalue=E2=80=9D to all standard =
associative=20
containers. It should work like =E2=80=9Coperator[]=E2=80=9D or =E2=80=9Cat=
=E2=80=9D function except that=20
it returns a default value when there is not such key in the container.
The signature is
T container::value(const Key& key, const T& default_value =3D T()) const;
for std::map, std::unordered_map, etc. and
T container::value(int index, const T& default_value =3D T()) const;
for std::vector, etc.
Qt users are already familiar with the =E2=80=9Cvalue=E2=80=9D function: al=
l the Qt=20
containers (QList, QMap, QHash, etc.) have it. Java provides similar=20
functionality via =E2=80=9Cget=E2=80=9D function. The =E2=80=9Cvalue=E2=80=
=9D function is very convenient=20
in many cases and its equivalent expression (find + if or ternary operator)=
=20
bloats the code a lot, so I no reason why we can't have it.
--=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_2283_14899533.1390861834408
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I propose to add a function called =E2=80=9Cvalue=E2=80=9D=
to all standard associative containers. It should work like =E2=80=9Copera=
tor[]=E2=80=9D or =E2=80=9Cat=E2=80=9D function except that it returns a de=
fault value when there is not such key in the container.<br><br>The signatu=
re is<br><br>T container::value(const Key& key, const T& default_va=
lue =3D T()) const;<br>for std::map, std::unordered_map, etc. and<br><br>T =
container::value(int index, const T& default_value =3D T()) const;<br>f=
or std::vector, etc.<br><br>Qt users are already familiar with the =E2=80=
=9Cvalue=E2=80=9D function: all the Qt containers (QList, QMap, QHash, etc.=
) have it. Java provides similar functionality via =E2=80=9Cget=E2=80=9D fu=
nction. The =E2=80=9Cvalue=E2=80=9D function is very convenient in many cas=
es and its equivalent expression (find + if or ternary operator) bloats the=
code a lot, so I no reason why we can't have it.<br></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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_2283_14899533.1390861834408--
.