问题描述
我明白std :: vector,std :: deque和
std :: list之间的区别是什么,
std :: vector可以在最后插入/删除数据。
std :: deque可以在结尾和开头插入/删除数据
和std :: list可以在任何地方插入/删除数据。
但是如何才能理解这些差异呢?
如果我理解正确,带有3个项目的std :: vector的数据会在另一个之后放置一个
。
V [0] V [1] V [2]
如果我添加另一个项目,V [0] V [1] V [2] V [3]
如果删除项目V [2]数据被移动以保持一个接一个,
V [0] V [1] V [3]
由于数据的移动,项目速度较慢。 />
使用std :: deque我们可以在块的前面插入数据,但是
与std :: vector有什么不同,我的意思是数据有移动方式相同
方式。
唯一的区别是它较慢,因为当你插入数据V [x]
时你需要移动整个存储块V [x] V [0] V [1] V [2]
最后std :: list与std :: vector相同,但是所有项目都是
指向实际元素的指针,所以如果我插入一个我需要做的所有元素是
将它添加到内存块的末尾并放置
元素的地址是一个有序的指针列表。所以,如果我有3个元素
V [0] V [1] V [2]
地址将具有正确的顺序A [0] = & V [2],A [1] =& V [0]和A [2]
=& V [1]。
这样我可以对物品进行分类或添加和删除它们。
列表中的物品不需要一个接一个地订购
地址。
我对容器的理解是否正确?
如果我的理解是正确的,为什么甚至打扰使用std :: vector和
std :: deque,std :: list似乎可以正常工作。
非常感谢。
Simon
事物向量提供随机访问迭代器,这意味着,除了其他东西,你可以从任何元素到
常量时间中的任何其他元素。随机访问的速度与随机插入的速度相比,实际上是向量和
列表之间的规范区别。还有一些STL算法需要随机访问
迭代器。同样从实际的角度来看,由于跟踪列表结构需要额外的指针,列表有一些开销。
Mark
你的理解是不正确的。得到一本更好的书。
V
哇,谢谢你的帮助!
我希望你能达到本月之后的职位数。
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
For one thing a vector provides random access iterators which means,
among other things, you can get from any element to any other element in
constant time. The speed of random access vs. the speed of random
insertion is really the canonical distinction between a vector and a
list. There are also certain STL algorithms which require random access
iterators. Also from a pracitcal point of view, lists have some
overhead due to the additional pointers needed to track the list structure.
Mark
Your understanding is incorrect. Get a better book.
V
Your understanding is incorrect. Get a better book.
V
wow, thanks for your help!
I hope you achieve the post count you are after this month.
Simon
这篇关于我对容器的理解是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!