c ++中stl_C ++ STL中的多图

c ++中stl

C ++中的多图 (multimap in C++)

C++ STL is an enriched functionality that is similar to Java Collection providing us the black-box of built-in data structures to use in our codes. It saves 100 lines to write and STL is a popular feature and advantage of C++ widely used in competitive programming.

C ++ STL是一种类似于Java Collection的丰富功能,为我们提供了可在代码中使用的内置数据结构的黑盒。 它节省了100行的写操作,并且STL是C ++的流行功能和优势,广泛用于竞争性编程中。

Those who are familiar with C++ STL must have used the STL map and unordered_map as part of their coding usage.

那些熟悉C ++ STL的人必须使用STL映射unordered_map作为其编码用法的一部分。

Multimap is an extended version of the map which can be very useful in many cases. Let's dig into the comparison b/w map and multimap to get proper understanding.

Multimap是地图的扩展版本,在许多情况下非常有用。 让我们深入研究黑白地图和多图,以获得适当的了解。

Let's follow the below example.

让我们遵循以下示例。

Say we have 5 keys with corresponding values like below,

假设我们有5个键,其对应值如下所示,

C++ STL | multimap (1)

Let's use STL map to store the above.

让我们使用STL映射来存储以上内容。

After inserting first <key, value> pair the map will be,

在插入第一个<key,value>对后,地图将成为,

C++ STL | multimap (2)

Now when we will insert the next <key, value> pair, we can see that the key already exists, so it will not make any new entry. It will simply update the key value in the map.

现在,当我们插入下一个<key,value>对时,我们可以看到该键已经存在,因此不会进行任何新输入。 它将仅更新地图中的键值。

C++ STL | multimap (3)

So, after similar insertion the final map will be,

因此,在类似的插入之后,最终图将是

C++ STL | multimap (4)

But what if we want all the entries persisting then what to do. Simply, maps will not work there. That's why we have a multimap in CPP STL. In multimap, we can have different entries based on even the same key. So, keys with different values will have a separate entry in multimap and that’s the power of multimap.

但是,如果我们希望所有条目都保留下来该怎么办。 简而言之,地图将无法在此处运行。 这就是为什么我们在CPP STL中有一个多图的原因。 在multimap中,即使是相同的键,我们也可以有不同的条目。 因此,具有不同值的键将在多重地图中具有单独的条目,这就是多重​​地图的威力。

So, whenever you need to store each distinct <key, value> pair we can use Multimap in such cases.

因此,无论何时需要存储每个不同的<key,value>对,在这种情况下我们都可以使用Multimap。

So after entering the above entries the final multimap will be like,

因此,在输入以上条目后,最终的多图将如下所示:

C++ STL | multimap (5)

Multimap also stores the keys in sorted order and has the same time complexity as the map.

Multimap还按排序顺序存储键,并且时间复杂度与Map相同。

To declare a multimap,

要声明多图,

multiplate <int,int> mymap;

Below are the functions available on multimap,

以下是多地图上可用的功能,

FunctionDescription
erase()It is used to erase the elements.
insert()It is used to insert elements.
begin()It is used to get an iterator to beginning.
end()It is used to get an iterator to end.
empty()It is used to check whether the multimap is empty or not.
size()It is used to get the size of the multimap.
maxsize()It is used to get the maximum size.
lower_bound()It is used to get the iterator to the lower bound.
upper_bound()It is used to get the iterator to the upper bound.
功能 描述
擦除() 用于擦除元素。
插() 它用于插入元素。
开始() 它用于使迭代器开始。
结束() 它用于使迭代器结束。
空() 用于检查多图是否为空。
尺寸() 它用于获取多图的大小。
maxsize() 它用于获取最大大小。
lower_bound() 它用于使迭代器达到下限。
upper_bound() 它用于使迭代器达到上限。

Read more...

阅读更多...

多图的C ++实现 (C++ implementation of multimap)

#include <bits/stdc++.h>
using namespace std;

int main()
{
    multimap<int, int> mymultimap;
    map<int, int> mymap;
    
    //insertion in map
    mymap[2] = 10;
    mymap[2] = 12;
    mymap[3] = 13;
    mymap[3] = 14;
    mymap[4] = 15;
    
    //insertion in multimap
    mymultimap.insert(make_pair(2, 10));
    mymultimap.insert(make_pair(2, 12));
    mymultimap.insert(make_pair(3, 13));
    mymultimap.insert(make_pair(3, 14));
    mymultimap.insert(make_pair(4, 15));

    cout << "Printing the map\n";

    for (auto it = mymap.begin(); it != mymap.end(); it++) {
        cout << "key: " << it->first << ", value: " << it->second << endl;
    }
    
    cout << "Printing the multimap\n";

    for (auto ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << ", value: " << ij->second << endl;
    }

    return 0;
}

Output:

输出:

Printing the map
key: 2, value: 12
key: 3, value: 14
key: 4, value: 15
Printing the multimap
key: 2, value: 10
key: 2, value: 12
key: 3, value: 13
key: 3, value: 14
key: 4, value: 15


翻译自: https://www.includehelp.com/stl/multimap-in-cpp-stl.aspx

c ++中stl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值