Reputation: 17266
Is there an equivalent of the at
function for iterators?
For example
std::vector example(10, 0);
const auto it = example.cbegin();
const auto it2 = it + 10;
std::cout << *it2 << std::endl; // does not throw
whereas with at
example.at(10); // throws
Upvotes: -1
Views: 109
Reputation: 7688
Iterators do not have at()
member function because it would violate "you only pay for what you use" C++ design principle. In a fully optimized build, a vector iterator is equivalent to a pointer. To support at()
, the iterator must carry valid range information - even when at()
is not used. There are implementation-specific debug builds with "checked iterators" that carry this extra info and fail an assert in case of invalid iterator use.
Upvotes: 1