How can std::vector access elements with huge gaps between them?
You've been fooled.
The iterator returned by std::back_inserter
has it++
as a no-op. So those 'gaps' you are creating? Yeah that's all doing nothing.
Your for-loop
for (int i = 0; i < 99999999; i++)
{
dest++;
}
does not do what you think. It has no effect on there, other than iterating from 0
to 99999999
.
When you look into the std::back_insert_iterator
, it says
[...]. Incrementing the
std::back_insert_iterator
is a no-op.
or as stated in 23.5.2.1.1 it simply returns the back_insert_iterator
, without doing anything into it.
constexpr back_insert_iterator& operator++(); constexpr back_insert_iterator operator++(int); #Returns: *this.
Meaning dest++;
has no effect. This makes entire assumptions you made, completely not valid. The program took long to execute only because of the iteration from 0
to 99999999
.
It raises the question: Then why there is an std::back_insert_iterator<Container>::operator++
overload at all?
From cpprefereence std::back_insert_iterator<Container>::operator++
:
Does nothing. These operator overloads are provided to satisfy the requirements of LegacyOutputIterator. They make it possible for the expressions
*iter++=value
and*++iter=value
to be used to output (insert) a value into the underlying container.
std::vector, is it c-style array?
Not quite, but the buffer that it creates is structurally identical.
when the address (pointers) of those element are not sequential?
The premise is faulty. The memory addresses of vector elements are contiguous. One object begins immediately after another.
Also, whether they are sequential or not doesn't matter. You can equally well iterate over a linked list even though those elements are not contiguous in memory.
OutputIterator ... But when you shift(add) it more then once between accessing them, then there is a gap
This assumption is not true.
In particular case of std::back_insert_iterator
, the documentation says:
std::back_insert_iterator<Container>::operator++
Does nothing.