C++ multisets 是 STL 容器,它们以 **排序** 的方式存储 **同一类型** 的元素,并且允许多个元素具有 **等效** 的 **值**。换句话说,允许重复值。
与 C++ sets 类似,每个元素的 **值** 就是其自身的 **键**。
Multiset 属性
C++ multisets 具有以下属性:
关联性: 元素通过其键引用,而不是通过其在容器中的绝对位置。
排序: 元素以排序的方式存储。
等效键: 容器中的多个元素可以具有等效的键。
不可修改: 元素的值在存储到 multiset 中后不能被修改。
创建 Multiset
要在 C++ 中实现 multiset,我们必须在程序中包含 <set>
头文件。
#include <set>
我们可以使用以下语法在 C++ 中创建 multiset:
// declare a multiset
std::multiset<data_type> multiset_name = {key1, key2, key3, ...};
这里,
std::multiset
- 声明一个类型为multiset
的 STL 容器。<data_type>
- 要存储在 multiset 中的值的数据类型。multiset_name
- 为 multiset 指定的唯一名称。key1, key2, key3, ...
- 要存储在 multiset 中的键/值。
例如,
// initialize multiset with elements
std::multiset<int> my_multiset1 = {5, 3, 8, 1, 3};
// create an empty multiset
std::multiset<int> my_multiset2;
注意: 接下来,我们将使用 std
命名空间,因此可以省略 std::
。
示例 1:创建 Multiset
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> my_multiset = {5, 3, 8, 1, 3};
for(int val : my_multiset) {
cout << val << " ";
}
return 0;
}
输出
1 3 3 5 8
在这里,我们创建了一个 int
类型的 **multiset**。
正如你所见,multiset 以排序的方式返回了值,并且包含了 **3** 的重复实例。
按降序排序 Multiset
要按降序获取 multiset 的元素,我们可以修改语法如下:
multiset<int, greater<int>> my_multiset ;
例如,
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int, greater<int>> my_multiset = {5, 3, 8, 1, 3};
for(int val : my_multiset) {
cout << val << " ";
}
return 0;
}
输出
8 5 3 3 1
从输出可以看出,multiset 现在以降序返回元素。
C++ Multiset 方法
在 C++ 中,我们有各种方法来执行 multiset 中的各种操作。
操作 | 描述 |
---|---|
insert() |
将元素插入 multiset。 |
erase() |
删除元素的所有实例。 |
clear() |
从 multiset 中移除所有元素。 |
empty() |
检查 multiset 是否为空。 |
size() |
返回 multiset 的大小。 |
示例 2:向 Multiset 添加元素
#include <iostream>
#include <set>
using namespace std;
int main () {
multiset<int> my_multiset;
// add values to the multiset
my_multiset.insert(10);
my_multiset.insert(30);
my_multiset.insert(20);
my_multiset.insert(50);
my_multiset.insert(40);
my_multiset.insert(50);
// print multiset after insertion
for (int i : my_multiset) {
cout << i << " ";
}
return 0;
}
输出
10 20 30 40 50 50
正如你所见,multiset 中的 insert()
与 sets 中的相同。
示例 3:从 Multiset 中移除元素
#include <iostream>
#include <set>
using namespace std;
int main () {
multiset<int> my_multiset = {10, 30, 20, 50, 40, 50};
// delete all occurences of 50 from the multiset
my_multiset.erase(50);
// print multiset after erase
cout << "\nThe multiset after erase: ";
for (int i : my_multiset) {
cout << i << " ";
}
// delete all values from the multiset
my_multiset.clear();
// print multiset after clear
cout << "\nThe multiset after clear: ";
for (int i : my_multiset) {
cout << i << " ";
}
return 0;
}
输出
The multiset after erase: 10 20 30 40 The multiset after clear:
正如你所见,clear()
在 multiset 中的操作与在 set 中相同,它会删除所有值。
另一方面,erase()
方法会从 multiset 中删除给定值的每个实例。
// delete all values from the multiset
my_multiset.erase(50);
在我们的例子中,my_multiset 中的 **50** 的两个实例都被删除了。
示例 4:C++ Multiset empty() 和 size() 方法
我们可以使用 **容量方法** empty()
和 size()
分别检查 multiset 是否为空以及获取其大小。
empty()
方法返回
- 1(true)- 如果 multiset 为空
- 0(false)- 如果 multiset 不为空
例如,
#include <iostream>
#include <set>
using namespace std;
int main () {
multiset<int> my_multiset = {10, 20, 20, 20 ,30 , 40};
// print multiset before clearing all values
cout << "The multiset before clear: ";
for (int i : my_multiset) {
cout << i << " ";
}
// check if the multiset is empty
cout << "\nEmpty: " << my_multiset.empty() << endl;
// check the size of the multiset
cout << "Size: " << my_multiset.size() << endl;
// delete all values from the multiset
my_multiset.clear();
// multiset after clear
cout << "\nThe multiset after clear: ";
for (int i : my_multiset) {
cout << i << " ";
}
// use the capacity methods again
cout << "\nEmpty: " << my_multiset.empty() << endl;
cout << "Size: " << my_multiset.size() << endl;
return 0;
}
输出
The multiset before clear: 10 20 20 20 30 40 Empty: 0 Size: 6 The multiset after clear: Empty: 1 Size: 0
在这里,我们在清空 multiset 内容之前和之后使用了 empty()
和 size()
方法。
清空内容之前
empty()
返回 **0**,表示 multiset 不为空。size()
返回 **6**,表示 multiset 中有六个元素。
清空内容之后
empty()
返回 **1**,表示 multiset 为空。size()
返回 **0**,表示 multiset 中没有元素。