一般来说,c++ 中的地图或矢量哪个更好?

发布于 2024-09-01 10:49:10 字数 161 浏览 3 评论 0原文

据我所知,访问向量中的元素需要恒定时间,而访问映射中的元素需要对数时间。然而,存储地图比存储矢量占用更少的内存。

所以我想问一下一般来说哪一个比较好?我正在考虑在我的程序中使用这两个之一,该程序包含大约 1000 个元素。我计划使用 3 维向量,它需要 1000x1000x1000 个元素。

As I know that accessing an element in vector takes constant time while in map takes logarithmic time. However, storing a map takes less memory than storing a vector.

Therefore, I want to ask which one is better in general? I'm considering using one of those two in my program, which has about 1000 elements. I plan to use 3 dimensional vector, which would take 1000x1000x1000 elements.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

朮生 2024-09-08 10:49:10

这个问题没有正确答案。正确的问题应该是“哪个更适合特定应用程序——在此处插入您的项目——”。要选择正确的容器,您需要解释它将如何使用。

There is no correct answer to this question. The correct question should be "which is better for the specific application --insert your project here--". To choose the correct container you need to explain how it will be used.

月隐月明月朦胧 2024-09-08 10:49:10

地图是一个关联容器——它提供与向量完全不同的功能。如果要将键与值关联起来,请使用映射。如果您的键只是非负连续整数,那么向量在各个方面都更优越。

A map is an associative container -- it serves a completely different function from a vector. Use a map if you want to associate keys with values. If your keys are just nonnegative consecutive integers, then a vector is superior in every way.

念三年u 2024-09-08 10:49:10

矢量和地图是用于不同目的的两个不同的容器。如果有一个更好,另一个就不会存在......

Vector and Map are two different containers used for different purposes. IF there was one better, the other would NOT exist ...

心凉 2024-09-08 10:49:10

首先,存储映射几乎肯定会比向量占用更多的内存,因为向量只是一个连续的块,而映射包含树结构。

关于哪个更好的问题的答案是,这取决于您要解决的问题。这实际上取决于您希望如何为数据建立索引。如果您的数据可以通过整数线性索引,那么向量将表现最好。

但是,在很多情况下您会希望以其他方式访问数据。例如,如果您想使用字符串对数据进行索引(如字典查找),那么地图的性能会更好。要使用向量对字符串数据进行索引,如果不保持向量排序,则必须执行线性搜索 (O(n)) 来查找元素。映射只需执行二分搜索 (O(log n)),随着 n 的增长,速度会快很多。

First of all, storing a map will almost definitely take more memory than a vector since a vector is simply a contiguous block and a map contains a tree structure.

The answer to your question of which is better is that it depends on the problem you are trying to solve. It really comes down to how you want to be able to index your data. If your data can be indexed linearly by an integer, then vector will perform the best.

However, there are many cases where you'll want to access your data in some other way. For example, if you want to index your data using a string (like a dictionary lookup), then the map will perform better. To index data with a string using a vector, you would have to perform a linear search (O(n)) to find an element if you don't keep the vector sorted. A map would only have to perform a binary search (O(log n)) which would be a LOT faster as n grows.

萌辣 2024-09-08 10:49:10

一般来说,没有更好的事情。这完全取决于您要执行的操作以及这些操作的频率。

对于相同数量的元素,向量不会比映射使用更多的空间。然而,这样的事情,例如稀疏矩阵,有时可以使用映射更有效地实现。

There is no such thing as better in general. It all depends on the operations You are going to perform and on the frequency of those operations.

Vectors don't use more space than map, for the same number of elements. However such a thing, like a sparse matrix for example, sometimes may be implemented more efficiently using map.

终陌 2024-09-08 10:49:10

如果您有稀疏的 3 维数组,则可以使用带有键作为索引组合的映射来节省大量内存。对于您的情况,索引 < 1000,key可以是32位无符号整数:

#include <stdint.h>
...
int index1 = 12, index2 = 34, index3 = 56;

// construct key:
uint32_t key = 
  ((index1&0x3FF) << 20) | 
  ((index2&0x3FF) << 10) | 
  (index3&0x3FF)
  );

// restore indexes:
index1 = (key >> 20) & 0x3FF;
index2 = (key >> 10) & 0x3FF;
index3 = (key) & 0x3FF;


If you have sparse 3-dimension array, you can save a lot of memory using map with the key as combination of indexes. For your case with indexes < 1000, key can be 32 bit unsigned integer:

#include <stdint.h>
...
int index1 = 12, index2 = 34, index3 = 56;

// construct key:
uint32_t key = 
  ((index1&0x3FF) << 20) | 
  ((index2&0x3FF) << 10) | 
  (index3&0x3FF)
  );

// restore indexes:
index1 = (key >> 20) & 0x3FF;
index2 = (key >> 10) & 0x3FF;
index3 = (key) & 0x3FF;


顾忌 2024-09-08 10:49:10

坦率地说,建议您创建 1,000,000,000 个空对象来存储 1000 个有用的对象,这是荒谬的。如果您认为向量会更快,那么当您考虑所有交换文件抖动、动态内存分配以及随之而来的不必要的对象构造时,您可能就大错特错了!

您的应用程序的描述可能太模糊;但如果一个值可以通过 x,y,z 索引;然后使用包含 x,y,z 的对象作为映射键。定位 1000 个物体并不需要很长时间 - 这是一张非常小的地图。

It is frankly rediculous to suggest that you creat 1,000,000,000, empty objects to store just 1000 useful ones. If you think a vector will be faster, you are probably very wrong when you consider all the swapfile thrashing, dynamic memory allocation, and unnecessary object construction that will ensue!

The description of your application is probably too vague; but if a value can be indexed by x,y,z; then use an object containing x,y,z as the map key. It will not take long to locate just 1000 objects - that is a very small map.

嘿嘿嘿 2024-09-08 10:49:10

如果你想要一个键值组件,请使用map。如果您想要按数字随机访问解决方案或迭代排序解决方案,请使用向量。

检查您的数据结构/算法分析文本以获取更多信息。

If you want a key-value component, use map. If you want a random-access-by-number solution, or an iteratively ordered solution, use vector.

Check your data structures/algorithm analysis text for more information.

不奢求什么 2024-09-08 10:49:10

我认为 vector<> 是容器的最佳默认选择(vector 除外)。如果我有理由更喜欢另一个容器,我会使用另一个;如果没有,我使用vector(或deque)。这已经是我能达到的“总体上更好”的最接近结果了。

所有 STL 容器都是有原因的,它们比任何其他容器都做得更好。如果不知道您将使用数据结构做什么,我无法提供更多帮助。

然而,使用错误结构的惩罚随着规模的增加而增加。如果您有几十个元素,任何类都可以。如果您谈论的是 10 亿个元素,您确实希望正确处理(并确保您的计算机能够处理它 - 您可能需要一个具有大量 RAM 的 64 位应用程序)。

I consider vector<> to be the best default choice of container (except for vector<bool>). If I have a reason to prefer another container, I use the other one; if not, I use vector<> (or deque<bool>). That's as close as I can get to "better in general".

There are reasons for all the STL containers, things they do better than any other one. Without knowing what you're going to be using the data structure for, I can't be any more help.

However, the penalties for using the wrong structure go up with the size. If you have a few dozen elements, any class will do. If you're talking about a billion elements, you really do want to get it right (and make sure your computer will handle it - you probably need a 64-bit app with a lot of RAM).

暖心男生 2024-09-08 10:49:10

Deque 是比映射更合适的向量替代品。但这取决于您的需求。向量需要线性内存来存储其所有数据,但双端队列则不需要。在大多数情况下,双端队列可以用作向量的替代品。

Deque is a more appropriate replacement for a vector than a map. But it depends on your needs. A vector requires linear memory for all its data, but a deque does not. And a deque is can be used as a drop in replacement for a vector in most cases.

女中豪杰 2024-09-08 10:49:10

如果您的 3D 矩阵填充稀疏(即大部分为零),那么您会对 boost::ublas::sparse_matrix。如果我没记错的话,默认情况下,它使用 std::map 作为底层容器。它提供了用于轻松行/列索引的运算符(以及行/列/元素迭代器)。

编辑:没关系,我以为 boost::ublas 有 3D 矩阵。看来并非如此。似乎 sparse_matrix 已被具有稀疏存储的新矩阵类型所取代。我已经很久没有使用那个库了。

您仍然可以查看 Boost.uBlas 来获取滚动您自己的稀疏 3D 矩阵的灵感。

If your 3D matrix will be sparsely populated (i.e. mostly zeros), then you'll be interested in boost::ublas::sparse_matrix. If I recall correctly, by default, it uses std::map as the underlying container. It provides operators for easy row/column indexing (as well as row/column/element iterators).

EDIT: Nevermind, I thought boost::ublas had 3D matrices. It seems that it doesn't. It also seems that sparse_matrix has been replaced by new matrix types having sparse storage. It's been a long time since I used that library.

You can still look at Boost.uBlas for inspiration in rolling your own sparse 3D matrix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文