Is it possible to use a reference as the value in a standard map container in C++?
If not - why not?
Example declaration:
map<int, SomeStruct&> map_num_to_struct;
Example usage:
...
SomeStruct* some_struct = new SomeStruct();
map_num_to_struct[3] = *some_struct;
map_num_to_struct[3].some_field = 14.3;
cout<<some_struct.some_field;
...
I would expect to see 14.3 printed...
To answer your question, passing a map value by reference will not cause problems.
No, it doesn't. It has pointers, but they're not quite the same thing. For more details about the differences between pointers and references, see this SO question.
HashMap put() Method in Java HashMap. put() method of HashMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value.
The C++ function std::map::operator== tests whether two maps are equal or not.
No. STL container value types need to be assignable. References are not assignable. (You cannot assign them a different object to reference.)
No, it's not. You can use pointers as the value type, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With