`std::multimap` 是否保证每个键的实际值在相等范围内?

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

“26.4.5.1 Class 模板多图概述”p1 的 C++ 标准说:

A multimap is an associative container that supports equivalent keys (possibly containing multiple copies of the same key value) and provides for fast retrieval of values of another type T based on the keys.

重点是我的。那么是不是说std::multimap在插入相等范围时可能不会保留原始key对象的副本,并用相等的替换它?

PS 为了明确这个问题是受此 的启发,我想知道 multimap 是否允许这样做,即我可以依靠它维护我的密钥的能力(这可能是相等但不相同)。

不,不是那个意思。 multimap 必须有一个与每个元素的对象关联的键。这里是规范的写法:

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.

由于 find 和朋友们需要 return 元素及其各自的键(同时保留等效元素的相对顺序!),一致的 multimap 无法存储单个键。

没有。该标准描述了如何在 this table 中插入元素。比如a_­eq.​emplace(​args)的效果描述如下(重点是我的)

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.

注意 std::multimap<Key, T>value_­typestd::pair<const Key, T>,因此必须构造相同(不仅是等效)的密钥。