我注意到在有效的STL
向量是这样一种序列 默认情况下应该使用。
这是什么意思?似乎忽略效率向量可以做任何事情。
谁能给我一个场景,其中向量不是一个可行的选择,但必须使用列表?
我注意到在有效的STL
向量是这样一种序列 默认情况下应该使用。
这是什么意思?似乎忽略效率向量可以做任何事情。
谁能给我一个场景,其中向量不是一个可行的选择,但必须使用列表?
当前回答
任何时候都不能使迭代器失效。
其他回答
当你想在容器之间移动对象时,你可以使用list::splice。
例如,一个图划分算法可以将常数数量的对象递归地划分到数量不断增加的容器中。对象应该初始化一次,并始终保持在内存中的相同位置。通过重新链接来重新排列比重新分配要快得多。
Edit: as libraries prepare to implement C++0x, the general case of splicing a subsequence into a list is becoming linear complexity with the length of the sequence. This is because splice (now) needs to iterate over the sequence to count the number of elements in it. (Because the list needs to record its size.) Simply counting and re-linking the list is still faster than any alternative, and splicing an entire list or a single element are special cases with constant complexity. But, if you have long sequences to splice, you might have to dig around for a better, old-fashioned, non-compliant container.
在表格中总结答案以供快速参考:
Vector | List | |
---|---|---|
Access | Faster | Slower |
Insert/Delete Operations | Slower | Faster |
Memory Allocation | Contiguous | Non-contiguous |
Size Pre-allocation | Need to be reserved | Not necessary to reserve |
Space Required Per Element | Only for the element itself | For element and pointers to next (and optionally previous elements) |
std::vector | std::list |
---|---|
Contiguous memory. | Non-contiguous memory. |
Pre-allocates space for future elements, so extra space required beyond what's necessary for the elements themselves. | No pre-allocated memory. The memory overhead for the list itself is constant. |
Each element only requires the space for the element type itself (no extra pointers). | Each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list. |
Can re-allocate memory for the entire vector any time that you add an element. | Never has to re-allocate memory for the whole list just because you add an element. |
Insertions at the end are constant, amortized time, but insertions elsewhere are a costly O(n). | Insertions and erasures are cheap no matter where in the list they occur. |
Erasures at the end of the vector are constant time, but for the rest it's O(n). | It's cheap to combine lists with splicing. |
You can randomly access its elements. | You cannot randomly access elements, so getting at a particular element in the list can be expensive. |
Iterators are invalidated if you add or remove elements to or from the vector. | Iterators remain valid even when you add or remove elements from the list. |
You can easily get at the underlying array if you need an array of the elements. | If you need an array of the elements, you'll have to create a new one and add them all to it, since there is no underlying array. |
一般来说,当你不关心你使用的是哪种类型的顺序容器时,使用vector,但如果你在容器的任何地方做很多插入或擦除,而不是在末尾,你就会想要使用list。如果你需要随机访问,你就需要向量,而不是列表。除此之外,在某些情况下,根据应用程序需要其中一个或另一个,但一般来说,这些都是很好的指导原则。
任何时候都不能使迭代器失效。
基本上,vector是一个具有自动内存管理功能的数组。数据在内存中是连续的。试图在中间插入数据是一项代价高昂的操作。
在列表中,数据存储在不相关的内存位置。在中间插入数据并不需要复制一些数据来为新数据腾出空间。
为了更具体地回答你的问题,我将引用这一页
对于访问元素以及从序列末尾添加或删除元素,向量通常是最有效的。对于涉及在末尾以外的位置插入或删除元素的操作,它们的性能不如deques和list,并且迭代器和引用的一致性也不如list。