当您需要将相同的键与不同的值存储在一起而映射无法执行相同操作时,可以使用Multimap。
在本文中,我们将看到如何在多映射中插入和删除?
Multimap还按排序顺序存储键,并且时间复杂度与Map相同。
要声明多图,
multiplate <int,int> mymap;
Insert()
插入件()函数是不相似的映射。在映射中,我们通过使用键作为索引像数组一样插入。
在多图中,您需要成对插入<key,value>对。
所以语法是
Iterator insert(pair<key,value>);
参数(一个或多个):一对键,值对。
返回值:迭代器插入的元素。
以下是插入示例,
让我们插入下面的<key,value>对,
Key Value 5 10 2 8 3 12 2 14 5 6
上面的插入将如下所示,
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));
在所有插入之后,将跟随多图,
Key Value 2 8 2 14 3 12 5 10 5 6
erase()
擦除功能与映射非常相似。该功能具有三种用法:
在多映射中,您需要提供键对以将其删除。它将删除多重映射中所有出现的键。
所以语法是
erase(key);
参数:键
返回值:void(仅删除所有出现的内容)
以下是删除示例,
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
在多映射中,您需要提供删除对的位置。它将在多图的确切位置删除。
所以语法是
erase(int position);
参数:位置
返回值:void(仅在该位置删除)
以下是此类删除的示例,
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
第三种删除是基于删除的。它删除范围内的条目。
mymap.erase(迭代器position1,迭代器position2)
参数:定义范围的开始和结束迭代器(第一个迭代器包含在内,最后一个迭代器除外)
返回值:void(仅在范围内删除)
代码和输出中提供了示例
insert()
,erase()
方法#include <bits/stdc++.h> using namespace std; int main(){ multimap<int, int> mymultimap; //在多图中插入 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 << "//键: " << 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 << "//键: " << 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 << "//键: " << 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 << "//键: " << ij->first << " ,value: " << ij->second << endl; } if (mymultimap.size() == 0) { cout << "the multimap is deleted\n"; } return 0; }
输出结果
Inserting like above example Printing the multimap //键: 2 ,value: 8 //键: 2 ,value: 14 //键: 3 ,value: 12 //键: 5 ,value: 10 //键: 5 ,value: 6 deleting key 2 Printing the multimap after deletion //键: 3 ,value: 12 //键: 5 ,value: 10 //键: 5 ,value: 6 deleting the first position entry Printing the multimap after deletion //键: 5 ,value: 10 //键: 5 ,value: 6 deleting the total range Printing the multimap after deletion the multimap is deleted