stl multimap_C ++ STL无序多图– std :: unordered_multimap

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.

由于无序多图使用哈希表存储键-值对,因此基于使用的内部哈希函数的时间复杂度。 它在平均情况下执行恒定的时间,在最坏的情况下,任何操作都将花费线性时间。

C ++ STL无序多图– std :: unordered_multimap (C++ STL Unordered Multimap – std::unordered_multimap)

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 <unordered_map>

#include <unordered_map>

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 directly initialize key – value pairs placing “=” symbol after declaration.

    我们可以直接在声明后直接初始化键值对,并在其中放置“ =”符号。
  • Using “insert” function also we can insert.

    使用“插入”功能,我们也可以插入。
  • We can make pair using std :: pair and we can insert this pair into unordered multimap.

    我们可以使用std :: pair进行配对 ,并且可以将此对插入到无序多图中。

  • Or we can directly move the pair.

    或者我们可以直接移动该对。
  • If there is another containers which have key – value pairs, by specifying a range we can copy those pairs into to unordered multimap.

    如果还有另一个具有键-值对的容器,则通过指定范围,我们可以将这些对复制到无序多图中。

erase()

擦除()

Same as insertion, we can perform deletion also in different ways.

与插入相同,我们也可以以不同的方式执行删除。

  • Deleting by pointing one of the pair using iterator.

    通过使用迭代器指向一对中的一个进行删除。
  • Deleting using “key”.

    使用“键”删除。

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 <iostream>
#include <string>
#include <unordered_map>
 
using namespace std;
 
int main ()
{
	unordered_multimap <string, int> unmmp1;
	unordered_multimap <string, int> :: 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 <string, int> ("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():

找():

  • Find is used to search a particular data pair.

    查找用于搜索特定的数据对。
  • We give key as a parameter to this function.

    我们将key作为此函数的参数。
  • If key is present iterator points to it’s position.

    如果存在键,则迭代器指向其位置。
  • If given key is not present iterator points to end of the container.

    如果不存在给定的密钥,则迭代器指向容器的末端。

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 <iostream>
#include <string>
#include <unordered_map>
 
using namespace std;
 
int main ()
{
	unordered_multimap <string, int> unmmp1;
	unordered_multimap <string, int> :: 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 <iostream>
#include <unordered_map>
 
using namespace std;
 
int main ()
{
	unordered_multimap <string, int> unmmp1, unmmp2;
	unordered_multimap <string, int> :: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值