c++ - 从 vector 切换到双端队列的大小限制通常是多少?

标签 c++ memory stl vector deque

我最近写了这篇文章:
How best to store VERY large 2D list of floats in c++? Error-handling?

一些人建议我将我的 2D 列表式 float 结构实现为 vector ,其他人则认为是双端队列。

据我所知,vector 需要连续内存,但因此效率更高。显然,如果可能的话,这是可取的。

因此,我的问题是,根据...

1. float
2. 整数

...在您应该从 vector 切换到 deque 以避免内存问题之前?

例如我正在寻找类似“在大约 400 万个 float 或 800 万个整数时,你应该切换......”的答案......如果可能的话。

最佳答案

好吧,这里有两种意见。 C++ 标准说 (23.1.1/2):

vector is the type of sequence that should be used by default.

list should be used when there are frequent insertions and deletions from the middle of the sequence.

deque is the data structure of choice when most insertions and deletions take place at the beginning or at the end of the sequence.

Herb Sutter argues the following (文章包含他的理由和性能分析):

I'd like to present an amiably dissenting point of view: I recommend that you consider preferring deque by default instead of vector, especially when the contained type is a class or struct and not a builtin type, unless you really need the container's memory to be contiguous.

关于c++ - 从 vector 切换到双端队列的大小限制通常是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3738362/

相关文章:

c++ - 当与可能导致缓冲区溢出的函数一起使用时,是否应该比其他变量更仔细地检查外部变量?

python-3.x - Webkit_server(从 python 的 dryscrape 调用)在访问每个页面时使用越来越多的内存。如何减少使用的内存?

linux - 如果遇到内存泄漏,在linux上会把内存占用计算到进程中吗?

memory - 在erlang中生成幻方时内存消耗过多 - 需要优化帮助

用于列表和 map 的 C++ 容器

c++ - 有没有办法将数据从 vector 移动到动态数组?

c++ - 有没有办法指定嵌套的 STL vector C++ 的维度?

c++ - 使用 include 定义枚举

c# - 创建和使用数据库并轻松安装的 GUI

c++ - 二叉树中最大的数