I understand that the standard allows std::vector<int, A>
to have the same type of iterators for different allocators A
. This is called SCARY iterators.
Now the question is does the standard allow std::vector<int, A>::iterator
be simply a typedef of A::pointer
, thus making it just an int*
for the default allocator?
Or is there some (implicit) requirement for it to be a separate class type per container? If there is no such requirement then why all major implementations (including the SCARY ones) don't use this approach? It would presumably reduce compiler work even further, though now code that overloads on int*
and vector<>::iterator
will not compile.
Convenience in programming: It is better to use iterators to iterate through the contents of containers as if we will not use an iterator and access elements using [ ] operator, then we need to be always worried about the size of the container, whereas with iterators we can simply use member function end() and iterate ...
Use of Iterators in C++ Iterator algorithms are not dependent on the container type. An iterator can be used to iterate over the container elements. It can also provide access to those elements to modify their values. Iterators follow a generic approach for STL container classes.
Re
” does the standard allow
std::vector<int, A>::iterator
be simply a typedef ofA::pointer
As far as I know, yes. But not std::vector<bool, A>
, because that's a specialization where a dereferenced iterator is a proxy object that accesses whatever representation is used, with the intent of supporting one bit per bool
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With