在STL中,list和vector的区别?

cutestar 2004-07-02 11:15:25
在STL中,list和vector有什么区别?在什么情况下该用那个?
...全文
868 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
journay 2004-07-02
  • 打赏
  • 举报
回复
list是链表,vector是数组
antijpn 2004-07-02
  • 打赏
  • 举报
回复
list<type>::iterator不能使用算术操作,而vector<type>::iterator可以
wendy188 2004-07-02
  • 打赏
  • 举报
回复
vector 支持的iterator是一个随机存取iterator
list 支持的iterator是一个双向iterator
Hxy129 2004-07-02
  • 打赏
  • 举报
回复
频繁的插入或删除操作用list,其他的用vector比较好,个人意见
hcj2002 2004-07-02
  • 打赏
  • 举报
回复
通俗一点来说:

list是链表(和链表的机制一样)。
vector像是一个数组。

BluntBlade 2004-07-02
  • 打赏
  • 举报
回复
list在物理存储上是分散的。
vector在物理存储上是连续的。
qwertasdfg123 2004-07-02
  • 打赏
  • 举报
回复
list是分散存贮的。
vector是连续存贮的。

如果你要进行插入、删除操作使用list相对较快,
而如果访问比较频繁,vector就快些。

cutestar 2004-07-02
  • 打赏
  • 举报
回复
看过《Thinking in C++ 2 edition Volume 2:Standard Libraries & Advanced Topics》?
在书里(英文版的,没找到中文的)提到有些区别,应该是:vector,list,deque:
vector:
The vector is intentionally made to look like a souped-up array, since it has array-style indexing but also can expand dynamically.vector is so fundamentally useful that it was introduced in a very primitive way early.
To achieve maximally-fast indexing and iteration, the vector maintains its storage as a single contiguous array of objects. This is a critical point to observe in understanding the behavior of vector. It means that indexing and iteration are lighting-fast, being basically the same as indexing and iterating over an array of objects. But it also means that inserting an object anywhere but at the end (that is, appending) is not really an acceptable operation for a vector.
list:
A list is implemented as a doubly-linked list and is thus designed for rapid insertion and removal of elements in the middle of the sequence (whereas for vector and deque this is a much more costly operation). A list is so slow when randomly accessing elements that it does not have an operator[ ]. It’s best used when you’re traversing a sequence, in order, from beginning to end (or end to beginning) rather than choosing elements randomly from the middle. Even then the traversal is significantly slower than either a vector or a deque, but if you aren’t doing a lot of traversals that won’t be your bottleneck.
该书建议使用vector。
有兴趣各位可以看看该书(www.BruceEckel.com)。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧