多重贴图与带集合的贴图

发布于 2024-12-03 14:48:16 字数 246 浏览 1 评论 0 原文

我想知道哪个更有效率。

std::map< String, std::set<int> >

std::multimap< String, int >

编辑: 我不打算对这些地图做任何不寻常的事情。标准插入、删除、修改、搜索。每个集合或多键字符串的大小不应超过 100。

I'm wondering which is more efficient.

std::map< String, std::set<int> >

or

std::multimap< String, int >

EDIT:
I do not plan on doing anything out of the ordinary with these maps. Standard insert, delete, modify, search. The size of each set or multi keyed String shouldn't be more than 100.

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

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

发布评论

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

评论(6

你好,陌生人 2024-12-10 14:48:16

我认为这取决于实现,但这是一个(未)受过教育的猜测:

实际上,它取决于您将在 multimapstd::set 中保留的整数数量代码>.在对键进行 log(n) 搜索之后,multimap 很可能会使用线性搜索值。如果您有大量整数值,则对键进行 log(n) 搜索,然后对值进行 log(n) 搜索可能会稍快一些。

然而,就效率而言,使用 string 键将任何内容存储在 mapmultimap 中几乎肯定会超过这两种情况的差异。

如下所述,multimap 可能会更易于使用且更易于维护,从而具有明显的优势。

This I believe is implementation dependant, but an (un)educated guess:

In practice it depends on the number of integers that you will be keeping in the multimap or the std::set. A multimap will most likely use a linear search of the values after the log(n) search of the key. If you have a large number of integer values, then the log(n) search of the keys followed by a log(n) search of the values may be slightly faster.

However, in terms of efficiency, storing anything in a map or multimap with a string key will almost certainly outweigh the differences in either case.

As said below, a multimap will likely be easier to use and more clear to maintain giving it a distinct advantage.

下壹個目標 2024-12-10 14:48:16

“set”选项将消除键+值对的重复,无论多重映射是否不会。

The "set" option will eliminate the key+value pairs duplications, whether the multimap won't.

悸初 2024-12-10 14:48:16

无论如何,它们实际上并不等同。 multimap 允许存储重复的键值对,而 map> 则不允许。

multimap<int, int> m;
m.insert(make_pair(2, 3));
m.insert(make_pair(2, 3)); // This changes the size of m!

否则

map<int, set<int>> m;
m[2].insert(3);
m[2].insert(3); // This does nothing.

因此,除非您需要重复的键值对, 我会使用 set 方法。语法也更好。我预计性能和内存使用方面的差异不会那么大。

They are actually not equivalent anyway. A multimap<X,Y> allows storing duplicate key-value pairs, whereas a map<T, set<X>> does not.

multimap<int, int> m;
m.insert(make_pair(2, 3));
m.insert(make_pair(2, 3)); // This changes the size of m!

Whereas

map<int, set<int>> m;
m[2].insert(3);
m[2].insert(3); // This does nothing.

So I would use the set approach unless you need duplicate key-value pairs. The syntax is also nicer. I expect the difference in performance and memory use is not that great.

最笨的告白 2024-12-10 14:48:16

如果您到目前为止对这些答案不满意(并不是说您不满意)并且我绝对被迫回答,我也会给出我有根据的“猜测”:

要插入,多重映射似乎更“有效” 。使用映射方法,您首先必须检索集合,然后对集合执行操作。
要删除/检索,地图似乎更“高效”。

If you're not happy with those answers so far (not saying that you are not) and I am absolutely forced to answer, I'll give my educated "guess" too:

To insert, the multimap appears to be more "efficient". With the map approach, you first have to retrieve, then perform operation on the set.
To delete/retrieve, map seems more "efficient".

猥︴琐丶欲为 2024-12-10 14:48:16

我不能肯定地说,但考虑到 multimap 的设计目的是执行另一个表达式的操作,最好具体化并使用 multimap,它更有意义,它还具有用于使用的成员函数多重映射作为一个概念,使用其他方法这些函数会有点时髦。

I can't say for sure but given that multimap was designed to do what the other is an expression of, it should be better to be specific and use the multimap, it makes a lot more sense, it also has member functions for working with a multimap as a concept, those functions would be a bit funky using the other approach.

近箐 2024-12-10 14:48:16

std::multimap<字符串、整数>很可能更有效率的内存。

std::multimap< String, int > is most likely more memory efficient.

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