2009年1月12日

vector, deque, and list

From: http://www.gotw.ca/gotw/054.htm

1. A deque offers constant-time insert() and erase() operations at the front of the container, whereas a vector does not -- hence the note in the Standard about using a deque if you need to insert or erase at both ends of the sequence.

2. A deque uses memory in a more operating system-friendly way, particularly on systems without virtual memory. For example, a 10-megabyte vector uses a single 10-megabyte block of memory, which is usually less efficient in practice than a 10-megabyte deque that can fit in a series of smaller blocks of memory.

3. A deque is easier to use, and inherently more efficient for growth, than a vector. The only operations supplied by vector that deque doesn't have are capacity() and reserve() -- and that's because deque doesn't need them! For vector, calling reserve() before a large number of push_back()s can eliminate reallocating ever-larger versions of the same buffer every time it finds out that the current one isn't big enough after all. A deque has no such problem, and having a deque::reserve() before a large number of push_back()s would not eliminate any allocations (or any other work) because none of the allocations are redundant; the deque has to allocate the same number of extra pages whether it does it all at once or as elements are actually appended.

Also from: http://www.velocityreviews.com/forums/t280389-vector-list-and-deque.html

When you need random access to the elements of the collection, and you will be doing insertions and deletions only at the back of the container. That is the time you should prefer vector.

Finally, total explanation at: http://blog.csdn.net/qer_liu/archive/2006/05/07/711642.aspx

沒有留言: