S
Simon
Hi,
I understand what one the differences between std::vector, std::deque and
std::list is,
the std::vector can have data inserted/deleted at the end.
The std::deque can have data inserted/deleted at the end and at beginning
and the std::list can have data inserted/deleted anywhere.
But how can those differences be physically understood?
If I understand correctly a std::vector with 3 items has data placed one
after the other.
V[0]V[1]V[2]
if I add another item it becomes, V[0]V[1]V[2]V[3]
and if a delete item V[2] the data is moved to remain one after the other,
V[0]V[1]V[3]
Because of the moving of data the item is slower.
With std::deque we can insert data at the front of the block, but how is
that different from std::vector, I mean the data has to be moved the same
way.
The only difference is that it is slower because when you insert data V[x]
you need to move the whole memory block V[x]V[0]V[1]V[2]
And finally the std::list is the same as a std::vector but all items are
pointers to the actual element so if I insert an element all I need to do is
to add it at the end of the block of memory and place the address of that
element is an ordered list of pointers. so if I have 3 elements
V[0]V[1]V[2]
the address will have the correct order A[0] = &V[2], A[1] = &V[0] and A[2]
= &V[1].
That way I can sort items or add and delete them.
The items in the list don't need to be one after the other really as the
address is ordered.
Is my understanding of containers correct?
If my understanding is correct why even bother about using std::vector and
std::deque, std::list seems to do the job ok.
Many thanks.
Simon
I understand what one the differences between std::vector, std::deque and
std::list is,
the std::vector can have data inserted/deleted at the end.
The std::deque can have data inserted/deleted at the end and at beginning
and the std::list can have data inserted/deleted anywhere.
But how can those differences be physically understood?
If I understand correctly a std::vector with 3 items has data placed one
after the other.
V[0]V[1]V[2]
if I add another item it becomes, V[0]V[1]V[2]V[3]
and if a delete item V[2] the data is moved to remain one after the other,
V[0]V[1]V[3]
Because of the moving of data the item is slower.
With std::deque we can insert data at the front of the block, but how is
that different from std::vector, I mean the data has to be moved the same
way.
The only difference is that it is slower because when you insert data V[x]
you need to move the whole memory block V[x]V[0]V[1]V[2]
And finally the std::list is the same as a std::vector but all items are
pointers to the actual element so if I insert an element all I need to do is
to add it at the end of the block of memory and place the address of that
element is an ordered list of pointers. so if I have 3 elements
V[0]V[1]V[2]
the address will have the correct order A[0] = &V[2], A[1] = &V[0] and A[2]
= &V[1].
That way I can sort items or add and delete them.
The items in the list don't need to be one after the other really as the
address is ordered.
Is my understanding of containers correct?
If my understanding is correct why even bother about using std::vector and
std::deque, std::list seems to do the job ok.
Many thanks.
Simon