C++ std::multimap<key, value> 和 std::map<key, std::set<value> 有什么区别 >

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8602068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:51:42  来源:igfitidea点击:

What's the difference between std::multimap<key, value> and std::map<key, std::set<value> >

c++mapcontainerskey-valuemultimap

提问by u1105178

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

我发现它们有一个键和多个唯一的值。

采纳答案by Bo Persson

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

多映射存储 (key, value) 对,其中键和值都可以出现多次。

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.

map<key, set<value>>将只存储一次特定键的每个值。为此,它必须能够比较值,而不仅仅是键。

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.

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

回答by typedef

A std::mapis an associative container, that allows you to have a unique key associated with your type value. For example,

Astd::map是一个关联容器,它允许您拥有与您的类型值关联的唯一键。例如,

void someFunction()
{
    typedef std::map<std::string, int> MapType;
    MapType myMap;

    // insertion
    myMap.insert(MapType::value_type("test", 42));
    myMap.insert(MapType::value_type("other-test", 0));

    // search
    auto it = myMap.find("test");
    if (it != myMap.end())
        std::cout << "value for " << it->first << " is " << it->second << std::endl;
    else
        std::cout << "value not found" << std::endl;
}

A std::multimapis equal to a std::map, but your keys are not unique anymore. Therefore you can find a range of items instead of just find one unique item. For example,

Astd::multimap等于 a std::map,但您的键不再是唯一的。因此,您可以找到一系列项目,而不仅仅是找到一个独特的项目。例如,

void someFunction()
{
    typedef std::multimap<std::string, int> MapType;
    MapType myMap;

    // insertion
    myMap.insert(MapType::value_type("test", 42));
    myMap.insert(MapType::value_type("test", 45));
    myMap.insert(MapType::value_type("other-test", 0));

    // search
    std::pair<auto first, auto second> range = myMap.equal_range("test");
    for (auto it = range.first; it != range.second; ++it)
        std::cout << "value for " << it->first << " can be " << it->second << std::endl;
}

The std::setis like an std::map, but it is not storing a key associated to a value. It stores only the key type, and assures you that it is unique within the set.

Thestd::set类似于 an std::map,但它不存储与值关联的键。它只存储密钥类型,并确保它在集合中是唯一的。

You also have the std::multiset, that follows the same pattern.

您还有std::multiset, 遵循相同的模式。

All these containers provide an O(log(n)) access with their find / equal_range.

所有这些容器都提供 O(log(n)) 访问及其 find / equal_range。

回答by Luka Rahne

map::insert

Because mapcontainers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value if so, the element is not inserted and its mapped value is not changed in any way.

因为map容器不允许有重复的键值,所以插入操作会检查每个插入的元素是否已经存在另一个具有相同键值的元素,如果存在,则不插入该元素并且不以任何方式更改其映射值。

on the other hand

另一方面

multimap::insert 

can insert any number of items with the same key.

可以插入任意数量的具有相同键的项目。

http://www.cplusplus.com/reference/stl/map/
http://www.cplusplus.com/reference/stl/multimap/

http://www.cplusplus.com/reference/stl/map/
http://www.cplusplus.com/reference/stl/multimap/

回答by Bj?rn Pollex

The latter requires that the values can be ordered (either via operator<or a comparison-function), the former does not.

后者要求可以对值进行排序(通过operator<或比较函数),前者不需要。