C ++ STL无序多集– std :: unordered_multiset

In this tutorial you will learn about STL Unordered Multiset container in c++ and all functions applicable on it.

在本教程中,您将了解C ++中的STL无序多集容器以及适用于它的所有函数。

As name says that, this is an unordered container. Set, unordered_set, multiset has some restrictions to store the elements. But here in unordered_multiset, the features are:

顾名思义,这是一个无序的容器。 Set,unordered_set,multiset对存储元素有一些限制。 但是在unordered_multiset中,这些功能是:

1) Elements need not follow specific order. They follow any order to store.

1)元素不必遵循特定的顺序。 他们遵循任何命令进行存储。

2) We know that unordered containers uses hash table for fast access. This also uses hash table to store elements.

2)我们知道无序容器使用哈希表进行快速访问。 这也使用哈希表来存储元素。

3) Facility that both value and key same.

3)价值和关键都相同的设施。

4) This dynamically handles the storage needs (i.e. on fly).

4)这可以动态处理存储需求(即即时)。

5) Unlike unordered sets, here duplicate values are allowed. It just counts how many such elements are there. By creating extra space.

5)与无序集不同,这里允许重复值。 它只计算有多少这样的元素。 通过创造额外的空间。

6) Using iterator positions only we can delete elements.

6)仅使用迭代器位置,我们可以删除元素。

C ++ STL无序多集– std :: unordered_multiset (C++ STL Unordered Multiset – std::unordered_multiset)

Iterators work on unordered multiset:

迭代器适用于无序多集:

begin(): returns iterator to the beginning

begin():将迭代器返回到开头

end(): returns iterator to the end of the list

end():将迭代器返回到列表的末尾

cbegin(): Returns constant iterator to beginning.

cbegin():将常量迭代器返回到开头。

cend(): Returns constant iterator to end.

cend():返回常量迭代器以结束。

To work with Unordred multiset we need to include unordered_set library.

要使用Unordred多集,我们需要包含unordered_set库。

#include<unordered_set>

#include <unordered_set>

Declaration:

宣言:

unordered_multiset <data_type> name; 

unordered_multiset <数据类型>名称;  

Let’s see some operations on unordered_multiset

让我们看看对unordered_multiset的一些操作

insert(): We can use UMSname.insert (element),  to insert. This will insert element at the front of the unordered multiset. Or we can directly initialize the values using braces {}.

insert():我们可以使用UMSname.insert(元素)进行插入。 这将在无序多重集的前面插入元素。 或者我们可以使用大括号{}直接初始化值。

size(): This gives the number of elements currently present in the unordered multiset.

size():这给出了当前在无序多重集中存在的元素数量。

max_size(): This will give the capacity of the unordered multiset. That how many elements in maximum we can insert.

max_size():这将提供无序多重集的容量。 我们最多可以插入多少个元素。

clear(): Clear operation will removes all elements in the unordered multiset and empty it.

clear():清除操作将删除无序多重集中的所有元素并将其清空。

empty(): This is Boolean function. That returns True (1) if unordered multiset is empty. Otherwise returns False (0).

empty():这是布尔函数。 如果无序多重集为空,则返回True(1)。 否则返回False(0)。

Example program to demonstrate above functions:

演示上述功能的示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std;
int main(){
unordered_multiset<int> ums;
unordered_multiset<int> :: iterator it;
ums = {1,2,3,4,5}; // directly intializing
ums.insert(100); // inserting a single element.
cout << "Elements in the unordered multiset are" << endl;
for(it= ums.begin(); it!=ums.end(); ++it) {
cout << *it << " "; // printing elements in unordered multiset;
}
cout << endl;
cout << "Size of the unordered multiset is " << ums.size() << endl;
cout << "Maximum size of the unordered multiset is " << ums.max_size() << endl;
// clear opeartin make unordered multiset empty.
cout << "Applying clear() opeartion....." << endl;
ums.clear();
cout << "Checking unordered multiset is empty or not" << endl;
bool flag= ums.empty();
if (flag ==1)
cout << "Unordered multiset is empty " << endl;
else
cout << "Unordered multiset is not empty " << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std ;
int main ( ) {
unordered_multiset < int > ums ;
unordered_multiset < int > :: iterator it ;
ums = { 1 , 2 , 3 , 4 , 5 } ; // directly intializing
ums . insert ( 100 ) ; // inserting a single element.
cout << "Elements in the unordered multiset are" << endl ;
for ( it = ums . begin ( ) ; it != ums . end ( ) ; ++ it ) {
cout << * it << " " ; // printing elements in unordered multiset;
}
cout << endl ;
cout << "Size of the unordered multiset is " << ums . size ( ) << endl ;
cout << "Maximum size of the unordered multiset is " << ums . max_size ( ) << endl ;
// clear opeartin make unordered multiset empty.
cout << "Applying clear() opeartion....." << endl ;
ums . clear ( ) ;
cout << "Checking unordered multiset is empty or not" << endl ;
bool flag = ums . empty ( ) ;
if ( flag == 1 )
cout << "Unordered multiset is empty " << endl ;
else
cout << "Unordered multiset is not empty " << endl ;
return 0 ;
}

Output

输出量

Elements in the unordered multiset are 5 4 3 100 2 1 Size of the unordered multiset is 6 Maximum size of the unordered multiset is 1152921504606846975 Applying clear() opeartion….. Checking unordered multiset is empty or not Unordered multiset is empty

无序多重集中的元素为 5 4 3 100 2 1 无序多重集中的大小 为6 无序多重集中的最大大小为1152921504606846975 应用clear()操作….. 检查无序多重 集中 是否为空无序多重 集中 为空

find(): If we apply find() on unordered multiset like this ums.find(element). If element is present, it will return an iterator pointing to that position. Otherwise it will return iterator pointing to end of the unordered multiset. This will be useful to detect weather element is present or not.

find():如果我们对ums.find(element)这样的无序多集应用find()。 如果存在element,它将返回指向该位置的迭代器。 否则,它将返回指向无序多重集末尾的迭代器。 这将有助于检测天气元素是否存在。

count(): If we apply count(element) on unordered multiset, it will return an integer, denoting that how many times element present in the unordered multiset.

count():如果我们将count(element)应用于无序多重集中,它将返回一个整数,表示元素在无序多重集中存在多少次。

Example program:

示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std;
int main(){
unordered_multiset<int> ums;
unordered_multiset<int> :: iterator it;
ums = {1,2,3,4,4,4,5}; // directly intializing
ums.insert(100); // inserting a single element.
it= ums.find(3);
if (it != ums.end()) {
cout << "Item  present " << endl;
}
else {
cout << "Item Not present " << endl;
}
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std ;
int main ( ) {
unordered_multiset < int > ums ;
unordered_multiset < int > :: iterator it ;
ums = { 1 , 2 , 3 , 4 , 4 , 4 , 5 } ; // directly intializing
ums . insert ( 100 ) ; // inserting a single element.
it = ums . find ( 3 ) ;
if ( it != ums . end ( ) ) {
cout << "Item  present " << endl ;
}
else {
cout << "Item Not present " << endl ;
}
return 0 ;
}

Output

输出量

Item present

礼物存在

erase(): Erase can be done in different ways.

delete():可以以不同的方式进行擦除。

  • Erase directly element: erase(element)

    直接擦除元素:擦除(元素)
  • Erase by pointing the iterator to particular element position.

    通过将迭代器指向特定元素位置进行擦除。
  • Erase range of values by giving beginning and ending pointers.

    通过提供开始和结束指针来擦除值的范围。

swap(): This function works on two unordered multisets. It move all elements of 1st unordered multiset to 2nd. And move all elements of 2nd unordered multiset to 1st one.

swap():此函数可用于两个无序多重集。 它将第一个无序多重集的所有元素移动到第二个 。 并将第二个无序多重集的所有元素移到一个。

Example program to show swap and erase operations:

显示交换和擦除操作的示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std;
int main(){
unordered_multiset<int> ums1;
unordered_multiset<int> ums2;
unordered_multiset<int> :: iterator it;
ums1 = {9, 8, 7, 6, 6, 6, 5, 4, 3};
ums2 = {99, 88, 77, 66, 55};
cout << "Elements in ums1 before swap are" << endl;
for(it= ums1.begin(); it!=ums1.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "Elements in the ums2 before swap are" << endl;
for(it= ums2.begin(); it!=ums2.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "Doing swap opeartion......." << endl;
ums1.swap(ums2);
cout << "Elements in ums1 after swap are" << endl;
for(it= ums1.begin(); it!=ums1.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "Elements in the ums2 after swap are" << endl;
for(it= ums2.begin(); it!=ums2.end(); ++it) {
cout << *it << " ";
}
cout << endl;
cout << "Erasing element 5 in ums2..." << endl;
ums2.erase(5);
cout << "Now erasing 1st element in ums2 by pointing iterator to it..." << endl;
it= ums2.begin();
ums2.erase(it);
cout << "Range erasing...[from: where element 6 found, To:End" << endl;
ums2.erase (ums2.find(6), ums2.end());
cout << "Elements in ums2 after doing above erase operations are" << endl;
for(it= ums2.begin(); it!=ums2.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iterator>
#include <unordered_set>
using namespace std ;
int main ( ) {
unordered_multiset < int > ums1 ;
unordered_multiset < int > ums2 ;
unordered_multiset < int > :: iterator it ;
ums1 = { 9 , 8 , 7 , 6 , 6 , 6 , 5 , 4 , 3 } ;
ums2 = { 99 , 88 , 77 , 66 , 55 } ;
cout << "Elements in ums1 before swap are" << endl ;
for ( it = ums1 . begin ( ) ; it != ums1 . end ( ) ; ++ it ) {
cout << * it << " " ;
}
cout << endl ;
cout << "Elements in the ums2 before swap are" << endl ;
for ( it = ums2 . begin ( ) ; it != ums2 . end ( ) ; ++ it ) {
cout << * it << " " ;
}
cout << endl ;
cout << "Doing swap opeartion......." << endl ;
ums1 . swap ( ums2 ) ;
cout << "Elements in ums1 after swap are" << endl ;
for ( it = ums1 . begin ( ) ; it != ums1 . end ( ) ; ++ it ) {
cout << * it << " " ;
}
cout << endl ;
cout << "Elements in the ums2 after swap are" << endl ;
for ( it = ums2 . begin ( ) ; it != ums2 . end ( ) ; ++ it ) {
cout << * it << " " ;
}
cout << endl ;
cout << "Erasing element 5 in ums2..." << endl ;
ums2 . erase ( 5 ) ;
cout << "Now erasing 1st element in ums2 by pointing iterator to it..." << endl ;
it = ums2 . begin ( ) ;
ums2 . erase ( it ) ;
cout << "Range erasing...[from: where element 6 found, To:End" << endl ;
ums2 . erase ( ums2 . find ( 6 ) , ums2 . end ( ) ) ;
cout << "Elements in ums2 after doing above erase operations are" << endl ;
for ( it = ums2 . begin ( ) ; it != ums2 . end ( ) ; ++ it ) {
cout << * it << " " ;
}
cout << endl ;
return 0 ;
}

Output

输出量

Elements in ums1 before swap are 3 4 5 6 6 6 7 8 9 Elements in the ums2 before swap are 55 66 77 88 99 Doing swap opeartion……. Elements in ums1 after swap are 55 66 77 88 99 Elements in the ums2 after swap are 3 4 5 6 6 6 7 8 9 Erasing element 5 in ums2… Now erasing 1st element in ums2 by pointing iterator to it… Range erasing…[from: where element 6 found, To:End Elements in ums2 after doing above erase operations are 4

交换之前ums1中的元素是 3 4 5 6 6 6 7 8 9 交换之前ums2中的元素是 55 66 77 88 99正在 执行交换操作……。 交换后ums1中的元素为 55 66 77 88 99 交换后ums2中的元素为 3 4 5 6 6 6 7 7 8 9 擦除ums2中的元素5… 现在通过将迭代器指向它来 擦除 ums2中的第一个元素… 范围擦除…[从:在找到 上述 元素6 之后,执行上述擦除操作后,ums2中的 To:End 元素为 4

翻译自: https://www.thecrazyprogrammer.com/2017/12/stl-unordered-multiset.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值