1. <i id='OF1Fy'><tr id='OF1Fy'><dt id='OF1Fy'><q id='OF1Fy'><span id='OF1Fy'><b id='OF1Fy'><form id='OF1Fy'><ins id='OF1Fy'></ins><ul id='OF1Fy'></ul><sub id='OF1Fy'></sub></form><legend id='OF1Fy'></legend><bdo id='OF1Fy'><pre id='OF1Fy'><center id='OF1Fy'></center></pre></bdo></b><th id='OF1Fy'></th></span></q></dt></tr></i><div id='OF1Fy'><tfoot id='OF1Fy'></tfoot><dl id='OF1Fy'><fieldset id='OF1Fy'></fieldset></dl></div>

        <small id='OF1Fy'></small><noframes id='OF1Fy'>

          <bdo id='OF1Fy'></bdo><ul id='OF1Fy'></ul>
        <tfoot id='OF1Fy'></tfoot>
        <legend id='OF1Fy'><style id='OF1Fy'><dir id='OF1Fy'><q id='OF1Fy'></q></dir></style></legend>

      2. 矢量或地图,使用哪一个?

        vector or map, which one to use?(矢量或地图,使用哪一个?)
        • <tfoot id='EPPs3'></tfoot>

          <legend id='EPPs3'><style id='EPPs3'><dir id='EPPs3'><q id='EPPs3'></q></dir></style></legend>
            <bdo id='EPPs3'></bdo><ul id='EPPs3'></ul>

                  <small id='EPPs3'></small><noframes id='EPPs3'>

                1. <i id='EPPs3'><tr id='EPPs3'><dt id='EPPs3'><q id='EPPs3'><span id='EPPs3'><b id='EPPs3'><form id='EPPs3'><ins id='EPPs3'></ins><ul id='EPPs3'></ul><sub id='EPPs3'></sub></form><legend id='EPPs3'></legend><bdo id='EPPs3'><pre id='EPPs3'><center id='EPPs3'></center></pre></bdo></b><th id='EPPs3'></th></span></q></dt></tr></i><div id='EPPs3'><tfoot id='EPPs3'></tfoot><dl id='EPPs3'><fieldset id='EPPs3'></fieldset></dl></div>
                    <tbody id='EPPs3'></tbody>

                  本文介绍了矢量或地图,使用哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我听很多人说,如果容器中预期元素的数量比较少,最好使用std::vector 而不是std::map 即使您将容器仅用于查找而不是迭代.

                  I've heard many people say that if the number of expected elements in the container is relatively small, it is better to use std::vector instead of std::map even if you were to use the container for lookups only and not iterating.

                  这背后的真正原因是什么?

                  What is the real reason behind this?

                  显然 std::map 的查找性能不会比 std::vector 差(尽管它可能以纳秒/微秒为单位不同)所以它有什么可与内存使用有关吗?

                  Obviously the lookup performance of std::map cannot be worse than std::vector (although it may differ in nanoseconds/microseconds) so does it have something to do with memory usage?

                  std::vector 在分割虚拟地址空间方面是否比 std::map 更好/更差?

                  Does std::vector fare any better/worse than std::map in fragmenting the virtual address space?

                  我正在使用 Visual Studio 附带的 STL 库(即 Microsoft 的实现).与其他实现相比,这有什么不同吗?

                  I am using the STL library that comes along with Visual Studio (i.e. Microsoft's implementation). Does that make any difference compared to other implementations?

                  推荐答案

                  我猜你在比较 mapvector.

                  I presume you're comparing map<A, B> with vector<pair<A, B> >.

                  首先,在一个非常小的向量中查找一个项目很容易比在地图中查找相同的东西要快,因为一个向量中的所有内存总是连续的(因此与计算机的缓存和诸如此类的东西一起玩得更好),并且在向量中查找某些内容所需的比较次数可能与地图大致相同.在非常大的容器的限制下,在地图中查找元素需要较少的操作.

                  Firstly, finding an item in a very small vector can easily be faster than the same thing in a map, because all the memory in a vector is always contiguous (and so plays more nicely with computers' caches and such things), and the number of comparisons needed to find something in a vector might be about the same as for a map. Finding an element in a map needs fewer operations in the limit of very large containers.

                  映射变得比向量更快的点取决于实现、处理器、映射中的数据以及处理器缓存中的内存等细微的事情.通常,地图变得更快的点大约是 5-30 个元素.

                  The point where maps become faster than vectors depends on the implementation, on your processor, what data is in the map, and subtle things like what memory is in the processor's cache. Typically, the point where map becomes faster would be about 5-30 elements.

                  另一种方法是使用哈希容器.它们通常被命名为 hash_mapunordered_map.名为 hash_map 的类不是官方标准的一部分(并且有一些变体);std::tr1::unordered_map 是.哈希映射通常比查找的普通映射更快,无论其中有多少元素,但实际上是否更快取决于键是什么,它是如何散列的,你必须处理什么值,以及如何处理键在 std::map 中进行比较.它不会像 std::map 那样以特定顺序保存事物,但您已经说过您不在乎这一点.我推荐散列映射,特别是如果键是整数或指针,因为这些散列非常快.

                  An alternative is to use a hash container. They are often named hash_map or unordered_map. Classes named hash_map are not part of the official standard (and there are a few variants out there); std::tr1::unordered_map is. A hash map is often faster than a normal map for lookups, regardless of how many elements are in it, but whether it is actually faster depends on what the key is, how it is hashed, what values you have to deal with, and how the key is compared in std::map. It doesn't keep things in a specific order like std::map, but you've said that you don't care about that. I'd recommend hash maps particularly if the keys are integers or pointers, because these hash very quickly.

                  这篇关于矢量或地图,使用哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                  vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                  C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                  Adding types to the std namespace(将类型添加到 std 命名空间)
                  Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                  How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)
                      <bdo id='wXXvV'></bdo><ul id='wXXvV'></ul>

                          <tbody id='wXXvV'></tbody>
                        <tfoot id='wXXvV'></tfoot>

                        <small id='wXXvV'></small><noframes id='wXXvV'>

                      • <i id='wXXvV'><tr id='wXXvV'><dt id='wXXvV'><q id='wXXvV'><span id='wXXvV'><b id='wXXvV'><form id='wXXvV'><ins id='wXXvV'></ins><ul id='wXXvV'></ul><sub id='wXXvV'></sub></form><legend id='wXXvV'></legend><bdo id='wXXvV'><pre id='wXXvV'><center id='wXXvV'></center></pre></bdo></b><th id='wXXvV'></th></span></q></dt></tr></i><div id='wXXvV'><tfoot id='wXXvV'></tfoot><dl id='wXXvV'><fieldset id='wXXvV'></fieldset></dl></div>

                          <legend id='wXXvV'><style id='wXXvV'><dir id='wXXvV'><q id='wXXvV'></q></dir></style></legend>