多重地图的每个元素都包含键和值吗? [英] Does Each Element of a multimap Contain Both the Key and Value?

查看:84
本文介绍了多重地图的每个元素都包含键和值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法想象没有被问到,但是我没有运气找到它.

I can't imagine this hasn't been asked, but I'm not having any luck finding it.

multimap的每个元素是否都包含其值其键?

Does each element of a multimap contain its value and its key?

multimap的内部结构看起来更像这样:

That is does the internal structure of a multimap look like more like this:

map<key, vector<value>>

或更像这样:

vector<pair<key, value>>

推荐答案

每个元素都包含其键和值.

Each element contains both its key and value.

您可以知道,因为迭代会返回对std::pair<Key const, Value>的稳定的非分配引用.

You can tell because iteration returns a stable non-allocating reference to std::pair<Key const, Value>.

此外,键可以比较相等但可以不同.多重映射允许您将额外数据"存储在该键中,而不是订购的一部分,然后您将其取回并将其与该值相关联.

What more, Keys can compare equal but be different. A multimap permits you to store "extra data" in that Key that isn't part of the ordering, and you'll get it back afterwards and it will be associated with that Value.

将多图实现为std::map<Key, std::vector<Value>>是有时比使用std::multimap<Key,Value>更好的选择.尤其是每个键具有多个值时,它将具有更高的内存效率,更好的局部性等.如果要统一执行此操作,则可能会比较棘手,但是如果要遍历由键组成的事物,则操作起来会更容易.

Implementing a multimap as a std::map<Key, std::vector<Value>> is an option that is sometimes better than using a std::multimap<Key,Value>. Especially with many-values per key, it will be more memory efficient, have better locality, etc. Iterating over it can be trickier if you want to do so uniformly, but if you want to iterate over things clumped by key it is easier.

这篇关于多重地图的每个元素都包含键和值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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