STL容器问题 [英] STL container question

查看:73
本文介绍了STL容器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要存储一些整数值,然后我会在

上搜索,看看它们是否存在于我的容器中。有人可以告诉我哪个容器会最快找到这些值吗?我不能使用

普通C数组(除非我把它设为2 ^ 32大小!)因为我不知道

max整数值。


感谢您的帮助


B2003

Hi

I need to store a number of integer values which I will then search on
later to see if they exist in my container. Can someone tell me which
container would be quickest for finding these values? I can''t use a
plain C array (unless I make it 2^32 in size!) since I don''t know the
max integer value.

Thanks for any help

B2003

推荐答案

Boltar写道:
Boltar wrote:

我需要存储一些整数值,然后我会搜索

以后查看它们是否存在于我的身上容器。有人可以告诉我哪个容器会最快找到这些值吗?我不能使用

普通C数组(除非我把它设为2 ^ 32大小!)因为我不知道

max整数值。
I need to store a number of integer values which I will then search on
later to see if they exist in my container. Can someone tell me which
container would be quickest for finding these values? I can''t use a
plain C array (unless I make it 2^32 in size!) since I don''t know the
max integer value.



先存储,然后排序,然后搜索(使用''std :: binary_search''),你

就可以使用''的std :: vector '的'。如果你希望在容器中搜索和更新

,''std :: set''可能更好,它的插入速度非常快。您在阅读标准库中的哪本书没有

在性能方面对不同标准容器进行了比较?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Store first, then sort, then search (using ''std::binary_search''), you
could just use ''std::vector''. If you expect both searching and updating
the container, ''std::set'' is probably better, its insertions are quite
fast. What book on the Standard Library are you reading that does not
have comparison of different standard containers in terms of performance?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Boltar schrieb:
Boltar schrieb:




我需要存储一些整数值,然后我将在稍后搜索
以查看它们是否存在于我的容器中。有人可以告诉我哪个容器会最快找到这些值吗?我不能使用

普通C数组(除非我把它设为2 ^ 32大小!)因为我不知道

max整数值。


感谢您的帮助


B2003
Hi

I need to store a number of integer values which I will then search on
later to see if they exist in my container. Can someone tell me which
container would be quickest for finding these values? I can''t use a
plain C array (unless I make it 2^32 in size!) since I don''t know the
max integer value.

Thanks for any help

B2003



std :: set


Lars

std::set

Lars


Boltar写道:
Boltar wrote:

我需要存储一些整数值,然后我将在之后搜索
以查看它们是否存在于我的容器中。有人可以告诉我哪个容器会最快找到这些值吗?我不能使用

普通C数组(除非我把它设为2 ^ 32大小!)因为我不知道

max整数值。
I need to store a number of integer values which I will then search on
later to see if they exist in my container. Can someone tell me which
container would be quickest for finding these values? I can''t use a
plain C array (unless I make it 2^32 in size!) since I don''t know the
max integer value.



排序向量。参见有效STL,第23项。


对于记录,你不会有2 ^ 32个整数,只有2 ^ 32位= 500 MiB。

它实际上并不是那么多RAM,具体取决于你的目标系统,并且

会让你检查O(1)复杂度的整数(而不是O(log
$ b $) b N))。

Sorted vector. See Effective STL, Item 23.

For the record, you wouldn''t 2^32 integers, just 2^32 bits = 500 MiB.
It''s actually not that much RAM, depending on your target system, and
would let you check for integers with O(1) complexity (rather than O(log
N)).


这篇关于STL容器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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