The difference between this member function and member operator function operator[] is that vector::at signals if the requested position is out of range by throwing an out_of_range exception.
Parameters
- n
- Position of an element in the vector.
If this is greater than the vector size, an exception of type out_of_range is thrown.
Notice that the first element has a position of 0, not 1.
Member type size_type is an unsigned integral type.
Return value
The element at the specified position in the vector.Member types reference and const_reference are the reference types to the elements of the vector container (for the default storage allocation model, allocator, these are T& and const T& respectively).
Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Output:
myvector contains: 0 1 2 3 4 5 6 7 8 9 |
Complexity
Constant.See also
| vector::operator[] | Access element (public member function) |
| vector::front | Access first element (public member function) |
| vector::back | Access last element (public member function) |
