在 C++ 中,无序多重集合 (unordered multisets) 存储元素的方式类似于多重集合,但没有特定的顺序,可以按值快速检索。
与多重集合一样,每个元素都充当其自身的键。
创建无序多重集合
要实现 C++ 中的多重集合,我们必须在程序中包含 <unordered_set>
头文件。
#include <unordered_set>
我们可以使用以下语法在 C++ 中创建无序多重集合
// declare an unordered multiset
std::unordered_multiset<data_type> multiset_name = {key1, key2, key3, ...};
这里,
std::unordered_multiset
- 声明一个类型为unordered_multiset
的 STL 容器。<data_type>
- 要存储在多重集合中的值的数据类型。multiset_name
- 为多重集合指定的唯一名称。key1, key2, key3, ...
- 要存储在多重集合中的键/值。
例如,
// initialize unordered multiset with elements
std::multiset<int> unordered_multiset1 = {5, 3, 8, 1, 3};
// create an empty unordered multiset
std::multiset<int> unordered_multiset2;
注意: 接下来,我们将使用 std
命名空间,这样我们就可以省略语法中的 std::
。
示例 1:创建无序多重集合
#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
unordered_multiset<int> new_multiset = {5, 3, 8, 1, 3};
for(int val : new_multiset) {
cout << val << " ";
}
return 0;
}
输出
1 8 3 3 5
在这里,我们创建了一个 int
类型的无序多重集合,可以看到元素没有按照任何特定顺序存储。
C++ 无序多重集合方法
以下是我们可以在 C++ 中与多重集合一起使用的一些常用方法。
函数 | 描述 |
---|---|
insert() |
将元素插入无序多重集合。 |
erase() |
删除元素的所有实例。 |
clear() |
从多重集合中移除所有元素。 |
empty() |
检查多重集合是否为空。 |
size() |
返回多重集合的大小。 |
found() |
查找值的出现次数。 |
count() |
计算值的频率。 |
示例 2:将元素插入无序多重集合
我们可以使用 insert()
函数向无序多重集合添加值。例如,
#include <iostream>
#include <unordered_set>
using namespace std;
int main () {
unordered_multiset<int> my_unordered_multiset;
// add values to the unordered_multiset
my_unordered_multiset.insert(10);
my_unordered_multiset.insert(30);
my_unordered_multiset.insert(20);
my_unordered_multiset.insert(50);
my_unordered_multiset.insert(40);
my_unordered_multiset.insert(50);
// print unordered_multiset after insertion
for (int i : my_unordered_multiset) {
cout << i << " ";
}
return 0;
}
输出
40 50 50 20 30 10
示例 3:从无序多重集合中删除值
我们可以使用 erase()
和 clear()
函数从无序多重集合中删除元素
erase()
- 删除所有实例...clear()
- 从多重集合中删除所有元素
例如,
#include <iostream>
#include <unordered_set>
using namespace std;
int main () {
unordered_multiset<int> my_unordered_multiset = {10, 30, 20, 50, 40, 50};
// delete all occurrences of 50
my_unordered_multiset.erase(50);
// print unordered_multiset after erase
cout << "\nThe unordered_multiset after erase: ";
for (int i : my_unordered_multiset) {
cout << i << " ";
}
// delete all values from the unordered_multiset
my_unordered_multiset.clear();
// print unordered_multiset after clear
cout << "\nThe unordered_multiset after clear: ";
for (int i : my_unordered_multiset) {
cout << i << " ";
}
return 0;
}
输出
The unordered_multiset after erase: 40 20 30 10 The unordered_multiset after clear:
示例 4:C++ empty() 和 size() 方法
我们可以使用 **容量方法** empty()
和 size()
来检查多重集合是否为空并获取其大小。
empty()
方法返回
- 1 (true) - 如果多重集合为空
- 0 (false) - 如果多重集合不为空
例如,
#include <iostream>
#include <unordered_set>
using namespace std;
int main () {
unordered_multiset<int> my_unordered_multiset = {10, 20, 20, 20 ,30 , 40};
// print unordered_multiset before clearing all values
cout << "The unordered_multiset before clear: ";
for (int i : my_unordered_multiset) {
cout << i << " ";
}
// check if the unordered_multiset is empty
cout << "\nEmpty: " << my_unordered_multiset.empty() << endl;
// check the size of the unordered_multiset
cout << "Size: " << my_unordered_multiset.size() << endl;
// delete all values from the unordered_multiset
my_unordered_multiset.clear();
// unordered_multiset after clear
cout << "\nThe unordered_multiset after clear: ";
for (int i : my_unordered_multiset) {
cout << i << " ";
}
// use the capacity methods again
cout << "\nEmpty: " << my_unordered_multiset.empty() << endl;
cout << "Size: " << my_unordered_multiset.size() << endl;
return 0;
}
输出
The unordered_multiset before clear: 40 30 20 20 20 10 Empty: 0 Size: 6 The unordered_multiset after clear: Empty: 1 Size: 0
示例 4:C++ find() 和 count() 方法
我们使用 find()
来检查给定值是否存在于多重集合中,使用 count()
来检查其频率。
例如,
#include <iostream>
#include <unordered_set>
using namespace std;
int main () {
unordered_multiset<int> my_unordered_multiset = {10, 20, 20, 20 ,30 , 40};
// print unordered_multiset before searching
cout << "The unordered_multiset: ";
for (int i : my_unordered_multiset) {
cout << i << " ";
}
// find an element in the unordered_multiset
int target = 20;
unordered_multiset<int>::iterator it = my_unordered_multiset.find(target);
if (it != my_unordered_multiset.end()) {
cout << "\nFound " << target << " in the unordered_multiset." << endl;
}
else {
cout << "\n" << target << " not found in the unordered_multiset." << endl;
}
// count occurrences of an element in the unordered_multiset
int count_value = 20;
int count_result = my_unordered_multiset.count(count_value);
cout << count_value << " appears " << count_result << " times in the unordered_multiset." << endl;
return 0;
}
输出
The unordered_multiset: 40 30 20 20 20 10 Found 20 in the unordered_multiset. 20 appears 3 times in the unordered_multiset.
注意上面程序中的这段代码
if (it != my_unordered_multiset.end()) {
上面的条件检查 find()
函数是否找到了该值。
如果找到了该值,it 迭代器将指向多重集合中与该值匹配的元素。
如果在多重集合中未找到该值,则 it 迭代器将等于 my_unordered_multiset.end()
,这表示它在到达多重集合的 **末尾** 而没有找到匹配项。
注意: end()
不指向实际元素,而是表示多重集合中最后一个元素之后的那个位置。