码迷,mamicode.com
首页 > 编程语言 > 详细

vector VS list in C++

时间:2016-01-19 17:12:29      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

 

vector is like ArrayList in Java.

vector:

  • Continuous memory.
  • Underlying array implementation.
  • Pre-allocate space for future element. This means extra space rather than necessary
  • Random access is fast, since looking up is index based.
  • No extra space for element. Just the element type itself.
  • Iterator is invalid after element insertions and erasures.
  • Insert element at the end is amortised constant time, while elsewhere is O(n).
  • Can re-allocate memory for the entire vector any time when you add an element.
  • Remove element at the end is constant time, while elsewhere is O(n).
  • Element erasure does not free memory

list:

  • Not continuous memory.
  • No pre-allocate space for future elements.
  • There are extra storage needed for node which holds the element, including pointers to the next and previous elements.
  • Insertion and deletion are cheap no matter where element is(No guarantee that list is faster. Needs to based on real application).
  • Access element can be expensive.
  • Iterator is still valid after insertion and deletion.

 

list is a WIN over vector in:

  • Guaranteed constant time for inserting element at the end, since vector can lead to memory allocation and copying of original elements. However, vector can get amortised constant time.
  • Insertion or erasures of elements in positions other than the end.

 

vector is a WIN over list in:

  • Random access
  • If you don‘t insert elements often, since vector has much better CPU cache locality than list, which means that accessing one element will make the next element very likely in the cache and can be retrieved without reading from slow RAM.

 

vector is like ArrayList in Java.

list is WIN over vector in:

  • Guaranteed constant time for inserting element at the end, since vector can lead to memory allocation and copying of original elements. However, vector can get amortised constant time.
  • Doing many insertions or erasures of elements in positions other than the end.

 

vector is WIN over list in:

  •  Random access

vector VS list in C++

标签:

原文地址:http://www.cnblogs.com/touchdown/p/5142490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!