gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:24:04 28 4
gpt4 key购买 nike

我最近写了这篇文章:
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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com