stl multimap
In this tutorial we will learn about stl unordered multimap container in C++.
在本教程中,我们将学习C ++中的stl无序多图容器。
Unordered multimap is an associative container. Same as unordered map it stores key and value pair. But unordered map doesn’t allow duplicate values. Here duplicate values are allowed. Since these are unordered containers, there is no order in the way of storing elements. But this allows duplicates so the elements which have same key are grouped together in the same bucket. For duplicate keys another count value is maintained with each key – value pair.
无序多图是一个关联容器。 与无序映射相同,它存储键和值对。 但是无序映射不允许重复值。 这里允许重复的值。 由于这些是无序容器,因此在存储元素方面没有顺序。 但这允许重复,因此具有相同键的元素会在同一存储桶中分组在一起。 对于重复的键,每个键-值对将维护另一个计数值。
Since unordered multimap uses hash table to store key – value pairs, time complexity based on internal hash function used. It performs constant time in average case and in worst case it will take linear time for any operation.
由于无序多图使用哈希表存储键-值对,因此基于使用的内部哈希函数的时间复杂度。 它在平均情况下执行恒定的时间,在最坏的情况下,任何操作都将花费线性时间。
Iterators that can be applicable on unordered multimap:
可以应用于无序多图的迭代器:
begin(): returns iterator to the beginning.
begin():将迭代器返回到开头。
end(): returns iterator to the end of the container.
end():将迭代器返回到容器的末尾。
cbegin(): Returns constant iterator to the beginning.
cbegin():将常数迭代器返回到开头。
cend(): Returns constant iterator to the end.
cend():将常量迭代器返回到末尾。
Unordered multimap is just an extra feature of unordered map. So we need to include same unordered map header function to work with unordered multimap.
无序多图只是无序图的一项附加功能。 因此,我们需要包含相同的无序地图标头函数,以使用无序多图。
i.e #include
即#include
Now see some functions applicable on unordered multimap:
现在看到一些适用于无序多图的功能:
insert()
我 nsert()
There are different types of insertions. In any type we should represent elements in a pair like structure. Since this stores key value pairs.
有不同类型的插入。 在任何类型中,我们都应以成对的结构表示元素。 由于这存储键值对。
i.e { “Key” , “value” } format.
即{“键”,“值”}格式。
Different inserting ways:
不同的插入方式:
We can make pair using std :: pair and we can insert this pair into unordered multimap.
我们可以使用std :: pair进行配对 ,并且可以将此对插入到无序多图中。
erase()
擦除()
Same as insertion, we can perform deletion also in different ways.
与插入相同,我们也可以以不同的方式执行删除。
clear(): Clear operation used to remove all key value pairs. By result container becomes empty.
clear():清除操作,用于删除所有键值对。 结果容器变空。
empty(): empty() is a Boolean function. It is used to check whether the container empty or not?
empty(): empty()是一个布尔函数。 用于检查容器是否为空?
Example program to show above functions:
显示上述功能的示例程序:
#include
#include
#include
using namespace std;
int main ()
{
unordered_multimap unmmp1;
unordered_multimap :: iterator it;
// direct assigning key - value pairs
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237}};
// inserting using "initializor list" function
unmmp1.insert ( {{"Sehwag" , 219} , {"Gayle" , 215}} );
// making pair and inserting
pair < string, int> hit2 ("Rohit" , 209);
unmmp1.insert (hit2);
// moving pair
unmmp1.insert (make_pair ("Rohit" , 208));
cout << "Unordered multimap contains: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Erasing first pair " << endl;
unmmp1.erase (unmmp1.begin());
cout << "Erasing record contains key as \"Guptil\" " << endl;
unmmp1.erase ("Guptil");
cout << "After performing above erase operations...." << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Applying clear() operation... " << endl;
unmmp1.clear();
cout << "Checking unordered multi map empty or not " << endl;
bool chk= unmmp1.empty();
if (chk == 1) cout << "Unordered multi map is empty " << endl;
else cout << "Unordered multi map is not empty " << endl;
return 0;
}
Output
输出量
Unordered multimap contains: (Gayle , 215) (Sehwag , 219) (Rohit , 208) (Rohit , 209) (Rohit , 264) (Guptil , 237) Erasing first pair Erasing record contains key as “Guptil” After performing above erase operations…. (Sehwag , 219) (Rohit , 208) (Rohit , 209) (Rohit , 264) Applying clear() operation… Checking unordered multi map empty or not Unordered multi map is empty
无序多图包含: (Gayle,215) (Sehwag,219) (Rohit,208) (Rohit,209) (Rohit,264) (Guptil,237) 擦除第一对 擦除记录包含键为“ Guptil” 在执行上述擦除操作之后…。 (Sehwag,219) (Rohit,208) (Rohit,209) (Rohit,264) 应用clear()操作… 检查无序 多图是否为空无序多图为空
size(): size() operation used to know how many key value pairs present in the unordered multimap.
size(): size()操作,用于了解无序多映射中存在多少个键值对。
max_size(): max_size() returns maximum size of the container.
max_size(): max_size()返回容器的最大大小。
find():
找():
count(): Count used will return how many times a pair present in the container with given key. We give key as a parameter to count function.
count():使用的Count将返回给定密钥在容器中存在一对的次数。 我们给键作为计数函数的参数。
Example program to show above functions:
显示上述功能的示例程序:
#include
#include
#include
using namespace std;
int main ()
{
unordered_multimap unmmp1;
unordered_multimap :: iterator it;
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Rohit", 209} , {"Rohit", 208} };
cout << "Unordered multimap contains: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << "Size of the unordered multimap is " ;
cout << unmmp1.size() << endl ;
cout << "Maximum Size of the unordered multimap is " ;
cout << unmmp1.max_size() << endl;
cout << "Finding \"Guptil data \" " << endl;
it = unmmp1.find("Guptil");
if (it!=unmmp1.end())
cout << "Key found and it's value is " << it->second << endl;
else
cout << "Data not found with given key " << endl;
cout << endl << "Counting how many times Rohit key is present " << endl;
cout << "Rohit scored 200+ runs " << unmmp1.count("Rohit") << " times " << endl;
return 0;
}
Output
输出量
Unordered multimap contains: (Guptil , 237) (Rohit , 208) (Rohit , 209) (Rohit , 264) Size of the unordered multimap is 4 Maximum Size of the unordered multimap is 329406144173384850 Finding “Guptil data ” Key found and it’s value is 237
无序多图包含: (Guptil,237) (Rohit,208) (Rohit,209) (Rohit,264) 无序多图的 大小 为4 最大无序多图的大小为329406144173384850 查找“ Guptil data” 找到的键,其值为237
Counting how many times Rohit key is present Rohit scored 200+ runs 3 times
计算Rohit密钥存在的次数 Rohit得分200+运行3次
swap(): swap() operation applied on two maps. Bu result all key – value pairs of unordered multi map1 are transferred to unordered multi map2 and vice –versa.
swap():应用于两个地图的swap()操作。 结果所有无序多重映射1的键-值对都被转移到无序多重映射2,反之亦然。
Example program to show swap operation:
显示交换操作的示例程序:
#include
#include
using namespace std;
int main ()
{
unordered_multimap unmmp1, unmmp2;
unordered_multimap :: iterator it;
unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Sehwag" , 219} , {"Rohit", 209} };
cout << "Unordered multimap :: 1 Before swap: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
cout << "Unordered multimap :: 2 Before swap: " << endl;
for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
cout << endl << "Performing swap operation " << endl;
unmmp1.swap(unmmp2);
cout << "Unordered multimap :: 1 After swap: " << endl;
for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
cout << "Unordered multimap :: 2 After swap: " << endl;
for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
cout << "(" << it->first << " , " << it->second << ")" << endl;
}
return 0;
}
Output
输出量
Unordered multimap :: 1 Before swap: (Sehwag , 219) (Guptil , 237) (Rohit , 209) (Rohit , 264) Unordered multimap :: 2 Before swap: (Ponting , 30) (Kohli , 32) (JayaSurya , 28) (Sachin , 49)
无序多图:: 1交换前: (Sehwag,219) (Guptil,237) (Rohit,209) (Rohit,264) 无序多图:: 2交换前: (Ponting,30) (Kohli,32) (JayaSurya,28 ) (萨钦(49岁)
Performing swap operation Unordered multimap :: 1 After swap: (Ponting , 30) (Kohli , 32) (JayaSurya , 28) (Sachin , 49) Unordered multimap :: 2 After swap: (Ponting , 30) (Kohli , 32) (JayaSurya , 28) (Sachin , 49)
执行交换操作 无序多图:: 1交换后: (Ponting,30) (Kohli,32) (JayaSurya,28) (Sachin,49) 无序多图:: :: 2交换后: (Ponting,30) (Kohli,32) ( JayaSurya,28岁 ; Sachin,49岁
翻译自: https://www.thecrazyprogrammer.com/2017/12/stl-unordered-multimap.html
stl multimap