有没有理由使用std :: list? [英] Is there ever a reason to use std::list?

查看:88
本文介绍了有没有理由使用std :: list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读此问题后并查看一些此处的结果,似乎应该完全避免使用C ++中的列表。我一直希望链表是我只需要遍历所有内容的情况下的选择容器,因为插入是指针操作的问题,并且永远不需要重新分配。



显然,由于缓存位置,列表的迭代速度非常慢,因此从使用较少的保留内存或更快的添加操作(从第二个链接看,似乎没有那么快)的任何好处)似乎不值得。



已经说过,从性能的角度来看,我什么时候应该使用 std :: list 超过 std :: deque 或(如果可能) std :: vector

另一方面, std :: forward_list 也会有很多缓存未命中吗?

解决方案


从性能的角度来看,何时应该使用 std :: list


从性能的角度来看,很少。想到的唯一情况是,如果您有许多列表需要拆分并合并以形成其他列表,则该列表为空。链表可以做到这一点而无需分配内存或移动对象。



list 的真正好处是稳定性:elements不需要移动,并且迭代器和引用除非引用了已擦除的元素,否则永远不会无效。


请注意, std :: forward_list 也会有很多缓存未命中吗?


是;仍然是一个链表。


After having read this question and looking at some results here, it seems like one should altogether completely avoid lists in C++. I always expected that linked lists would be the containers of choice for cases where I only need to iterate over all the contents because insertion is a matter of pointer manipulation and there is never a need to reallocate.

Apparently, because of "cache locality," lists are iterated over very slowly, so any benefit from having to use less reserve memory or faster addition (which is not that much faster, it seems, from the second link) doesn't seem worth it.

Having said that, when should I, from a performance standpoint, use std::list over std::deque or, if possible, std::vector?

On a side note, will std::forward_list also have lots of cache misses?

解决方案

When should I, from a performance standpoint, use std::list

From a performance standpoint, rarely. The only situation that comes to mind is if you have many lists that you need to split and join to form other lists; a linked list can do this without allocating memory or moving objects.

The real benefit of list is stability: elements don't need to be movable, and iterators and references are never invalidated unless they refer to an element that's been erased.

On a side note, will std::forward_list also have lots of cache misses?

Yes; it's still a linked list.

这篇关于有没有理由使用std :: list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆