Does `std::multimap` guarantee actual value of each key in equal range?

爱⌒轻易说出口 提交于 2019-12-06 11:00:23

No. The standard describes how an element is inserted in this table. For example, the effect of a_­eq.​emplace(​args) is described as follows (emphasis mine).

Effects: Inserts a value_­type object t constructed with std​::​forward<​Args​>(​args)... and returns the iterator pointing to the newly inserted element. If a range containing elements equivalent to t exists in a_­eq, t is inserted at the end of that range.

Note the value_­type of std::multimap<Key, T> is std::pair<const Key, T>, so a same (not only equivalent) key must be constructed.

No, it doesn't mean that. multimap has to have a key associated with the object for every element. Here is normative wording:

26.4.5.1: A multimap is an associative container that supports equivalent keys (possibly containing multiple copies of the same key value)...

26.2.6: The phrase “equivalence of keys” means the equivalence relation imposed by the comparison object. That is, two keys k1 and k2 are considered to be equivalent if for the comparison object comp, comp(k1, k2) == false && comp(k2, k1) == false. [ Note: This is not necessarily the same as the result of k1 == k2. — end note ] For any two keys k1 and k2 in the same container, calling comp(k1, k2) shall always return the same value.

Since find and friends are required to return elements with their respective keys (also preserving relative ordering of equivalent elements!), there is no way conformant multimap can store a single key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!