Element Access
Method |
---|
MapContainer |
Access and throw exception if it doesn't exist |
mapped_type &at(const key_type &k); |
const mapped_type &at(const key_type &k) const; |
Access and create new element if it doesn't exist |
mapped_type &operator[] (const key_type &k); |
mapped_type &operator[] (key_type &&k); |
template <typename... Targs> mapped_type &operator()(const dimension_type &x1, const Targs &...xs); |
Parameters
k
- the key of the element to findx1
- the value of the element to find in the first dimensionxs
- the value of the element to find in other dimensions
Return value
A reference to the element associated with that key.
Exceptions
std::out_of_range
if the container does not have an element with the specified key
Complexity
Notes
While the at
function throws an error when the element is not found, operator[]
creates a new element with that key if the element is not found. Like other libraries that handle multidimensional data, we use the operator()
for element access as a convenience because the operator[]
does not allow multiple parameters. We can still use operator[]
with a front::key_type
though.
Note
Like std::map
, and unlike std::multimap
, spatial containers implement the element access operators even though duplicate keys are permitted. The reason std::multimap
does not implement these operators is because the operator might be ambiguous when there is more than one element that matches the given key.
By convention we formally remove this ambiguity by always using the first element that matches that key. It's up to the library user to decide if this behaviour is appropriate for their application. If not, the modifier functions should be used instead.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
1 |
|