std::multimap&lt;key,value&gt; 有什么区别?和 std::map<key, std::set<value>;&gt;

2022-01-24 00:00:00 key-value containers c++ map multimap

我发现他们只有一个键和多个值.

I found that they have one key and multiple values which is unique.

推荐答案

multimap 存储成对的 (key, value),其中 key 和 value 都可以出现多次.

The multimap stores pairs of (key, value) where both key and value can appear several times.

map> 只会为特定键存储每个值一次.为此,它必须能够比较值,而不仅仅是键.

The map<key, set<value>> will only store each value once for a specific key. To do that, it will have to be able to compare the values, not just the keys.

比较相等的值是否相等,或者您是否希望单独存储它们取决于您的应用程序.也许它们包含不同但不参与集合比较的字段.

It depends on your application if the values that compare equal are equivalent, or if you wish to store them separately anyway. Perhaps they contain fields that are different but do not take part in the comparison for the set.

相关文章