insert stl_C ++ STL中的多图insert(),擦除()

insert stl

C ++ multimap :: insert()和multimap :: erase()函数 (C++ multimap::insert() and multimap::erase() functions)

Multimap is used when you need to store the same keys with distinct values where the map fails to do the same.

当需要将相同的键与不同的值存储在一起而映射无法执行相同操作时,可以使用Multimap。

In this article, we are going to see how to insert and delete in multimap?

在本文中,我们将看到如何在多地图中插入和删除?

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;

多图Insert() (Multimap Insert())

The insert() function is not similar to the map. In the map, we insert like an array by using the key as an index.

insert()函数与映射不同。 在地图中,我们通过使用键作为索引像数组一样插入。

In multimap, you need to insert <key, value> pair as a pair.

在多图中,您需要成对插入<key,value>对。

So the syntax is,

所以语法是

Iterator insert(pair<key,value>);

Parameter(s): Pair of key, value pair.

参数: 对, 对。

Return value: Iterator to the inserted element.

返回值:插入元素的迭代器。

Below is an example of insertion,

以下是插入示例,

Let's insert the below <key, value> pairs,

让我们插入下面的<key,value>对,

Key	Value
5	10
2	8
3	12
2	14
5	6

The above insertion will be like below,

上面的插入将如下所示,

1)  Declare the multimap
    multimap <int,int> mymap;
2)  Insert the first key
    mymap.insert(make_pair(5,10));
3)  Insert the first key
    mymap.insert(make_pair(2,8));
4)  Insert the first key
    mymap.insert(make_pair(3,12));
5)  Insert the first key
    mymap.insert(make_pair(2,14));
6)  Insert the first key
    mymap.insert(make_pair(5,6));

After all the insertion the multimap will be following,

在所有插入之后,将跟随多图,

Key	Value
2	8
2	14
3	12
5	10
5	6

多图擦除() (Multimap erase())

The erase function is quite similar to the map. The function has three kinds of usage:

擦除功能与地图非常相似。 该功能具有三种用法:

1.擦除密钥的所有条目 (1. Erase all the entries of the key)

In multimap, you need to provide the key for the pair to delete. It will delete all occurrences of the key in the multimap.

在多地图中,您需要提供密钥对以将其删除。 它将删除多重映射中所有出现的键。

So the syntax is,

所以语法是

erase(key);

Parameter(s): key

参数 :键

Return value: void (Simply deletes all the occurrences)

返回值 :void(仅删除所有出现的内容)

Below is an example of deletion,

以下是删除示例,

Let's delete entries for key=2

Multimap is already constructed by the insert function

Now,
    mymap.erase(2)

This will erase both the entries with the key 

Resultant map would be:

Key	Value
3	12
5	10
5	6

2.仅删除基于位置的单个值 (2. Erasing only a single value based on position)

In multimap, you need to provide the position for the pair to delete. It will delete at the exact position in the multimap.

在多地图中,您需要提供删除对的位置。 它将在多图的确切位置删除。

So the syntax is,

所以语法是

erase(int position);

Parameter(s): position

参数 :位置

Return value: void (Simply deletes at the position)

返回值 :void(仅在该位置删除)

Below is an example of such deletion,

以下是此类删除的示例,

Let's delete the first entry of the multimap 
(after previous deletion what we got)

    mymap.erase(mymap.begin())

It would delete the entry with key 3. 
So the resultant multimap would be

Key	Value
5	10
5	6

3.删除范围 (3. Deletion in range)

There is a third kind of deletion which is based on deletes. It deletes entries within the range.

第三种删除是基于删除的。 它删除范围内的条目。

mymap.erase(iterator position1, iterator position2)

mymap.erase(迭代器position1,迭代器position2)

Parameter(s): starting and ending iterators to define a range (the first iterator is inclusive and the last one is exclusive)

参数 :定义范围的开始结束迭代器(第一个迭代器包含在内,最后一个迭代器除外)

Return value: void (Simply deletes within the range)

返回值 :void(仅在范围内删除)

Example is provided in the code and output

代码和输出中提供了示例

多地图insert(),ease()函数的C ++实现 (C++ implementation of multimap insert(), erase() functions)

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

int main()
{
    multimap<int, int> mymultimap;
    //insertion in multimap

    cout << "Inserting like above example\n";
    mymultimap.insert(make_pair(5, 10));
    mymultimap.insert(make_pair(2, 8));
    mymultimap.insert(make_pair(3, 12));
    mymultimap.insert(make_pair(2, 14));
    mymultimap.insert(make_pair(5, 6));

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

    cout << "deleting key 2\n";
    mymultimap.erase(2);
    cout << "Printing the multimap after deletion\n";

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

    cout << "deleting the first position entry\n";
    mymultimap.erase(mymultimap.begin());

    cout << "Printing the multimap after deletion\n";

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

    cout << "deleting the total range\n";
    mymultimap.erase(mymultimap.begin(), mymultimap.end());

    cout << "Printing the multimap after deletion\n";

    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }
 
    if (mymultimap.size() == 0) {
        cout << "the multimap is deleted\n";
    }

    return 0;
}

Output

输出量

Inserting like above example
Printing the multimap
key: 2 ,value: 8
key: 2 ,value: 14
key: 3 ,value: 12
key: 5 ,value: 10
key: 5 ,value: 6
deleting key 2
Printing the multimap after deletion
key: 3 ,value: 12
key: 5 ,value: 10
key: 5 ,value: 6
deleting the first position entry
Printing the multimap after deletion
key: 5 ,value: 10
key: 5 ,value: 6
deleting the total range
Printing the multimap after deletion
the multimap is deleted


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

insert stl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值