Since this is a linked list, you should probably use std::list...
The rule of thumb is that you want to use a linked list when you need to insert elements into random positions in the list, or delete random elements from the list. If you mainly need to add/delete elements to/from the end of the list, then you should use std::vector. If you need to add/delete elements to/from either beginning or the end of the list, then you should use std::deque.
Keep in mind, we are talking about probabilities here. If you need to insert an element into the middle of an std::vector once in a blue moon, that will probably be ok. But if you need to do this all the time, it will have a major impact on performance, because the vector will need to constantly move its elements, and probably reallocate its memory too.
On the other hand, the advantage of using a vector is that its elements are contiguous in memory, which greatly improves performance if you simply need to traverse them in order because of caching.
Since the data in this list is pointers, why bother with a linked list at all? For small PODs, std::vector is usually the best first bet, and due to the better locality of its data playing nicely with processor caches it often out-performs a linked list even where, in theory, a linked list should be better. I'd pick std::vector until some profiling would show that there is a performance problem and std::list performs better.
发布评论
评论(3)
由于这是一个链接列表,您可能应该使用 std::list...
经验法则是,当您需要将元素插入列表中的随机位置或从列表中删除随机元素时,您需要使用链表。如果您主要需要在列表末尾添加/删除元素,那么您应该使用
std::vector
。如果您需要在列表的开头或结尾添加/删除元素,那么您应该使用std::deque
。请记住,我们在这里讨论的是概率。如果您需要千载难逢地将一个元素插入到 std::vector 的中间,那可能没问题。但如果您需要一直这样做,则会对性能产生重大影响,因为向量需要不断移动其元素,并且可能还需要重新分配其内存。
另一方面,使用向量的优点是它的元素在内存中是连续的,如果你只需要因为缓存而按顺序遍历它们,这会大大提高性能。
Since this is a linked list, you should probably use std::list...
The rule of thumb is that you want to use a linked list when you need to insert elements into random positions in the list, or delete random elements from the list. If you mainly need to add/delete elements to/from the end of the list, then you should use
std::vector
. If you need to add/delete elements to/from either beginning or the end of the list, then you should usestd::deque
.Keep in mind, we are talking about probabilities here. If you need to insert an element into the middle of an
std::vector
once in a blue moon, that will probably be ok. But if you need to do this all the time, it will have a major impact on performance, because the vector will need to constantly move its elements, and probably reallocate its memory too.On the other hand, the advantage of using a vector is that its elements are contiguous in memory, which greatly improves performance if you simply need to traverse them in order because of caching.
既然这个列表中的数据是指针,为什么还要使用链表呢?对于小型 POD,
std::vector
通常是最好的选择,并且由于其数据的更好的局部性,可以与处理器缓存很好地配合,因此它的性能通常优于链表,即使在理论上,链表应该更好。我会选择std::vector
,直到某些分析表明存在性能问题并且std::list
性能更好。Since the data in this list is pointers, why bother with a linked list at all? For small PODs,
std::vector
is usually the best first bet, and due to the better locality of its data playing nicely with processor caches it often out-performs a linked list even where, in theory, a linked list should be better. I'd pickstd::vector
until some profiling would show that there is a performance problem andstd::list
performs better.请参阅此处:
http://linuxsoftware.co.nz/cppcontainers.html
有一个流程图帮助您在底部选择合适的容器。
See here:
http://linuxsoftware.co.nz/cppcontainers.html
There's a flow chart to help you choose the right container at the bottom.