From: Helmut Jarausch on
Hi,

what happens if I try to access a non-existent key by using
operator[] ?

e.g.
#include <unordered_map>
typedef std::unordered_map<int,int> IntHash;
typedef IntHash::value_type ValuePair;

int main() {
IntHash H;
H.insert(ValuePair(3,9));
int v = H[7];
cout << v << endl;
}

gcc-4.4.4 does not throw an exception and returns 0.
What should it do? Throwing an exception or returning a
default value (if this, how to set the default value)?

Many thanks for a hint,
Helmut.


--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 22 Jul., 01:30, Helmut Jarausch <jarau...(a)igpm.rwth-aachen.de>
wrote:
> Hi,
>
> what happens if I try to access a non-existent key by using
> operator[] ?
>
> e.g.
> #include <unordered_map>
> typedef std::unordered_map<int,int> IntHash;
> typedef IntHash::value_type ValuePair;
>
> int main() {
> IntHash H;
> H.insert(ValuePair(3,9));
> int v = H[7];
> cout << v << endl;
>
> }
>
> gcc-4.4.4 does not throw an exception and returns 0.
> What should it do? Throwing an exception or returning a
> default value (if this, how to set the default value)?

This member function is similarly specified as
the corresponding operator[] of std::[multi]map:
If not found, a value-initialized (aka default-
constructed) key will be inserted.

There is no way to specify a different default
key. If you prefer an exception, you should
use the new member function at(), which throws
std::out_of_range if no such element is present.

HTH & Greetings from Bremen,

Daniel Kr�gler


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Lailoken on
On Jul 21, 4:30 pm, Helmut Jarausch <jarau...(a)igpm.rwth-aachen.de>
wrote:
> Hi,
>
> what happens if I try to access a non-existent key by using
> operator[] ?
>
> e.g.
> #include <unordered_map>
> typedef std::unordered_map<int,int> IntHash;
> typedef IntHash::value_type ValuePair;
>
> int main() {
> IntHash H;
> H.insert(ValuePair(3,9));
> int v = H[7];
> cout << v << endl;
>
> }
>
> gcc-4.4.4 does not throw an exception and returns 0.
> What should it do? Throwing an exception or returning a
> default value (if this, how to set the default value)?

As far as I am aware:

..operator[key]

is equivalent to:

..insert( make_pair(key, mapped_type()).first->second

for both map and unordered map.

Marius.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 22 Jul., 03:00, Daniel Kr�gler <daniel.krueg...(a)googlemail.com>
wrote:
> If not found, a value-initialized (aka default-
> constructed) key will be inserted.
>
> There is no way to specify a different default
> key.

There are two typos in these sentences: Please replace "key" by
"mapped value" in the above, the remainder should
be correct.

Sorry for any confusion,

Daniel




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Davit on
accessing a non-existent key by using [], will add new entry in the
map with your key and default value of your value type(or created with
non-argument constructor);

in your code, after line "int v = H[7];" H.size() will be 2.



On Jul 22, 3:30 am, Helmut Jarausch <jarau...(a)igpm.rwth-aachen.de>
wrote:
> Hi,
>
> what happens if I try to access a non-existent key by using
> operator[] ?
>
> e.g.
> #include <unordered_map>
> typedef std::unordered_map<int,int> IntHash;
> typedef IntHash::value_type ValuePair;
>
> int main() {
> IntHash H;
> H.insert(ValuePair(3,9));
> int v = H[7];
> cout << v << endl;
>
> }
>
> gcc-4.4.4 does not throw an exception and returns 0.
> What should it do? Throwing an exception or returning a
> default value (if this, how to set the default value)?
>
> Many thanks for a hint,
> Helmut.
>
> --
> Helmut Jarausch
> Lehrstuhl fuer Numerische Mathematik
> RWTH - Aachen University
> D 52056 Aachen, Germany



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]