How to reinforce-iterate by referencing an array element that is of type set (STL) of user-defined objects?
necklace
is a c-array of elements std::set<Pearl>
, where Pearl
is a user-defined type.
necklace
is defined as follows
std::set<Pearl> necklace[NUM_CIRC] = { };
After that, the items are filled with Pearl
.
Next snippet
for (Pearl &p : necklace[circular_coordinate]) {
p.set_color("white");
}
results in compilation error
invalid initialization of reference of type 'Pearl &' from expression of type "const Pearl"
How can I access necklace[circular_coordinate]
non-contact mode?
source to share
Unfortunately, elements are std::set
immutable. This means that you cannot change them by design.
The reason for this is the same as with immutable keys in std::set
and std::map
- changing the key can lead to a change in the order between the keys, which is not supported by associative containers.
source to share