我对容器的理解是否有效? [英] Is my understanding of containers valid?

查看:73
本文介绍了我对容器的理解是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我明白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

解决方案

Simon写道:



我明白std :: vector之间有什么区别, std :: deque和
std :: list是,
std :: vector可以在最后插入/删除数据。


您可以在任何地方插入/删除,但是如果您最后这样做,则只能保证O(1)

(摊销)。 br />
std :: deque可以在结尾和开头插入/删除数据


再次,你可以在任何地方插入/删除,但它是'只在

开始和结束时分摊O(1)。

和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不同,我的意思是数据必须移动相同的
w ay。
唯一的区别是它更慢,因为当你插入数据V [x]
时,你需要移动整个存储块V [x] V [0] V [1] V [2 ]


您正在对实施细节做出假设。前插入

到deque是摊销O(1) - 实施如何管理

任意,只要它满足容器的要求。
<最后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似乎可以正常工作。




事物向量提供随机访问迭代器,这意味着,除了其他东西,你可以从任何元素到

常量时间中的任何其他元素。随机访问的速度与随机插入的速度相比,实际上是向量和

列表之间的规范区别。还有一些STL算法需要随机访问

迭代器。同样从实际的角度来看,由于跟踪列表结构需要额外的指针,列表有一些开销。


Mark


Simon写道:

我理解std :: vector,std :: deque
和std :: list之间的区别是什么,
std :: vector可以在最后插入/删除数据。


std :: vector可以在任何位置插入数据。

std :: deque可以在最后插入/删除数据并在


同样的事情,std :: deque你可以在任何地方插入。

开头,std :: list可以在任何地方插入/删除数据。


是的。

但这些差异怎么能被理解?


没有差异。

如果我理解正确[...]


你不要'' t。

我对容器的理解是否正确?


No.

如果我的理解是正确的,为什么甚至懒得使用
std :: vector和std :: deque,std :: list似乎做得很好。




你的理解是不正确的。得到一本更好的书。


V


>>如果我的理解是正确的,为什么甚至打扰使用

std :: vector和std :: deque,std :: list似乎可以正常工作。


您的理解不正确。得到一本更好的书。

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

解决方案

Simon wrote:

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.
You can insert/delete anywhere, however it''s only guaranteed O(1)
(amortized) if you do so at the end.
The std::deque can have data inserted/deleted at the end and at beginning
Again, you can insert/delete anywhere, but it''s amortized O(1) at the
beginning and end only.
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]
You''re making assumptions about implementation details. Front insertion
into a deque is amortized O(1)-- how the implementation manages that is
arbitrary provided it satisfies the requirements on the container.

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.



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


Simon wrote:

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.
std::vector can have data inserted at any position.
The std::deque can have data inserted/deleted at the end and at
Same thing, std::deque you can insert anywhere.
beginning and the std::list can have data inserted/deleted anywhere.
Yes.
But how can those differences be physically understood?
There are no differences.
If I understand correctly [...]
You don''t.
Is my understanding of containers correct?
No.
If my understanding is correct why even bother about using
std::vector and std::deque, std::list seems to do the job ok.



Your understanding is incorrect. Get a better book.

V


>> If my understanding is correct why even bother about using

std::vector and std::deque, std::list seems to do the job ok.



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


这篇关于我对容器的理解是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆