Container Subscripting

Many of the containers allow subscripting; the list does not. A list is a linear linked structure intended to be scanned, not accessed randomly. The array, vector and map can all be subscripted. Subscripting can be performed with square brackets or the at()} method. Either can appear on either side of an assignment.

Subscripting with square brackets, av[i]:
  • An out-of-bounds array array or vector reference using brackets is unchecked and dangerous. No exception is thrown. Instead, the operation is completed (in error) at some location outside the structure.
  • Subscripting a map with a non-existent key is fine. The key is simply added to the map.
Subscripting with av.at(i):
  • An out-of-bounds array array or vector reference is detected and throws a std::out_of_range exception.
  • Subscripting a map with a non-existent key also throws a std::out_of_range exception. (Which doesn't really make sense, since keys don't belong to a range.)

For arrays and vectors, the at method provides bounds-checking. For maps, the usefulness of at simply follows from the details of the application: which behavior of subscripting helps.