无序多集及其应用

我们讨论过 无序集 在我们之前的文章中,无序_集的问题是,不可能在该数据结构中存储重复的条目。例如,如果无序_集中已经有一些值v,再次插入v将没有效果。 为了处理这种重复,应该使用无序的_multiset,它还可以存储重复的元素。在内部插入现有值时,数据结构会增加与每个值关联的计数。由于每个值的计数都存储在无序_多集中,所以它比无序_集(如果所有值都不同)占用更多空间。 unordered_multiset的内部实现与unordered_set相同,也使用哈希表进行搜索,只是计数值与前一个中的每个值相关联。由于元素的散列,它没有存储元素的特定顺序,所以所有元素都可以以任何顺序出现,但重复元素会出现在一起。无序_多重集上的所有操作平均需要恒定的时间,但在最坏的情况下可能会达到线性。 Unordered_multiset支持以下代码中演示的许多功能:

null

C

// C++ program to demonstrate various function
// of unordered_multiset
#include <bits/stdc++.h>
using namespace std;
// making typedef for short declaration
typedef unordered_multiset< int >::iterator umit;
// Utility function to print unordered_multiset
void printUset(unordered_multiset< int > ums)
{
//  begin() returns iterator to first element of set
umit it = ums.begin();
for (; it != ums.end(); it++)
cout << *it << " " ;
cout << endl;
}
//  Driver program to check all function
int main()
{
//  empty initialization
unordered_multiset< int > ums1;
//  Initialization by intializer list
unordered_multiset< int > ums2 ({1, 3, 1, 7, 2, 3,
4, 1, 6});
//  Initialization by assignment
ums1 = {2, 7, 2, 5, 0, 3, 7, 5};
//  empty() function return true if set is empty
// otherwise false
if (ums1.empty())
cout << "unordered multiset 1 is empty" ;
else
cout << "unordered multiset 1 is not empty" ;
//  size() function returns total number of elements
// in data structure
cout << "The size of unordered multiset 2 is : "
<< ums2.size() << endl;
printUset(ums1);
ums1.insert(7);
printUset(ums1);
int val = 3;
// find function returns iterator to first position
// of  val, if exist otherwise it returns iterator
// to end
if (ums1.find(val) != ums1.end())
cout << "unordered multiset 1 contains "
<< val << endl;
else
cout << "unordered multiset 1 does not contains "
<< val << endl;
//  count function returns total number of occurrence in set
val = 5;
int cnt = ums1.count(val);
cout << val << " appears " << cnt
<< " times in unordered multiset 1 " ;
val = 9;
//  if count return >0 value then element exist otherwise not
if (ums1.count(val))
cout << "unordered multiset 1 contains "
<< val << endl;
else
cout << "unordered multiset 1 does not contains "
<< val << endl;
val = 1;
// equal_range returns a pair, where first is iterator
// to first position of val and second it iterator to
// last position to val
pair<umit, umit> erange_it = ums2.equal_range(val);
if (erange_it.first != erange_it.second)
cout << val << " appeared atleast once in "
"unoredered_multiset " ;
printUset(ums2);
// erase function deletes all instances of val
ums2.erase(val);
printUset(ums2);
// clear function deletes all entries from set
ums1.clear();
ums2.clear();
if (ums1.empty())
cout << "unordered multiset 1 is empty" ;
else
cout << "unordered multiset 1 is not empty" ;
}


输出:

unordered multiset 1 is not emptyThe size of unordered multiset 2 is : 93 0 5 5 7 7 2 2 3 0 5 5 7 7 7 2 2 unordered multiset 1 contains 35 appears 2 times in unordered multiset 1 unordered multiset 1 does not contains 91 appeared atleast once in unoredered_multiset 6 4 2 7 3 3 1 1 1 6 4 2 7 3 3 unordered multiset 1 is empty

正如我们所见,大多数操作工作与无序_集类似,但需要注意的是: equal_range(val)函数返回一对类型,其中第一个迭代器指向val的第一个位置,第二个指向数据结构中val的最后一个位置。 erase(val)函数从数据结构中删除其所有实例,例如,如果某个值v在无序_multiset中出现了t次,并且在调用erase时,v被完全删除,这多次不是预期的行为。 通过使用find函数和erase的迭代器版本,我们只能删除某个值的一个副本,因为find函数将迭代器返回到找到的值的第一个位置,我们可以通过该迭代器进行擦除,而不是通过实际值来仅删除一个副本,执行此操作的代码如下所示:

CPP

//  C++ program to delete one copy from unordered set
#include <bits/stdc++.h>
using namespace std;
//  making typedef for short declaration
typedef unordered_multiset< int >::iterator umit;
//  Utility function to print unordered_multiset
void printUset(unordered_multiset< int > ums)
{
// begin() returns iterator to first element of
// set
umit it = ums.begin();
for (; it != ums.end(); it++)
cout << *it << " " ;
cout << endl;
}
//  function to delete one copy of val from set
void erase_one_entry(unordered_multiset< int >& ums,
int val)
{
//  find returns iterator to first position
umit it = ums.find(val);
//  if element is there then erasing that
if (it != ums.end())
ums.erase(it);
}
//  Driver program to check above function
int main()
{
//  initializing multiset by initializer list
unordered_multiset< int > ums ({1, 3, 1, 7, 2, 3,
4, 1, 6});
int val = 1;
printUset(ums);
erase_one_entry(ums, val);
printUset(ums);
}


输出:

6 4 2 7 3 3 1 1 1 6 4 2 7 3 3 1 1 

无序_多集的方法:

  • 插入() –在无序的_multiset中插入新元素。因此增加了容器的大小。
  • 开始 –返回一个迭代器,该迭代器指向容器中的第一个元素或容器中的第一个元素。
  • 结束() –返回一个迭代器,该迭代器指向容器中最后一个元素之后的位置,或指向容器中最后一个元素之后的位置。
  • 空的() –如果无序的_multiset容器为空,则返回true。否则,返回false。
  • 查找() –返回一个迭代器,该迭代器指向包含元素val的位置。
  • cbegin() –返回一个常量迭代器,该迭代器指向容器中的第一个元素或容器中的第一个元素。
  • cend() –返回一个常量迭代器,该迭代器指向容器中最后一个元素之后的位置,或指向容器中最后一个元素之后的位置。
  • 等_范围() –返回所有元素等于给定值的范围。
  • 安置() –在无序的_multiset容器中插入新元素。
  • 清除() –清除无序的_multiset容器中的内容。
  • 计数() –返回无序_multiset容器中等于给定值的元素计数。
  • 大小() –unordered_multiset的size()方法用于计算调用它的unordered_集合的元素数。
  • 最大尺寸 –unordered_multiset的max_size()取unordered_multiset容器能够容纳的最大元素数。
  • 互换 –交换两个无序的多组容器的内容物。
  • 抹去 –用于删除单个元素或具有确定值的所有元素,或删除从开始(包含)到结束(排除)的一系列元素。
  • 桶() –返回给定元素所在的桶号。桶大小从0到桶计数-1不等。
  • 水桶大小() –返回包含元素val的存储桶中的元素数。
  • 储备 –unordered_multiset的reverse()函数将容器中的桶数(bucket_count)设置为最合适的,以包含至少n个元素。
  • 最大桶数 –返回无序多集容器可以拥有的最大桶数。
  • 荷载系数() –返回无序_多集容器中的当前负载系数。
  • 最大载荷系数() –返回无序_多集容器的最大负载系数。
  • 桶数 –返回无序的_multiset容器中的桶总数。
  • hash_函数() –该散列函数是一元函数,它只接受一个参数,并基于该参数返回一个唯一的size_t类型值。
  • rehash() –将容器中的桶数设置为N或更多。
  • 键_eq() –根据比较返回布尔值。
  • 安放_hint() –在无序的_multiset容器中插入新元素。
  • 获取分配程序 –此函数获取存储的分配器对象,并返回用于构造容器的分配器对象。
  • 接线员= – ‘=’是C++ STL中的一个操作符,它将一个无序的多集复制(或移动)到另一个无序的多集和无序的多集::运算符=相应的操作符函数。

最近关于无序多集的文章 本文由Utkarsh Trivedi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享