📅  最后修改于: 2020-10-19 01:13:36             🧑  作者: Mango
Multiset是C++ STL(标准模板库)的一部分。Multiset是类似于Set的关联容器,用于存储排序的值(该值本身是T类型的键),但与Set仅存储唯一键的Set不同,multiset可以具有重复键。默认情况下,它使用<运算符比较键。
可以插入或删除多集中元素的值,但不能更改(元素始终为const)。
template < class T, // multiset::key_type/value_type
class Compare = less, // multiset::key_compare/value_compare
class Alloc = allocator // multiset::allocator_type
> class multiset;
T:存储在容器多重集中的元素类型。
比较:比较类,它接受两个相同类型的布尔变量,并返回一个值。此参数是可选的,二进制谓词less是默认值。
Alloc:用于定义存储分配模型的分配器对象的类型。
让我们看一个示例来演示C++ Multiset:
#include
#include
#include
#include
using namespace std;
int main()
{
multiset ms;
multiset::iterator it, it1, msIt;
int choice, item;
while (1)
{
cout<<"\n---------------------"<>choice;
switch(choice)
{
case 1:
cout<<"Enter value to be inserted: ";
cin>>item;
if (ms.empty())
it1 = ms.insert(item);
else
it1 = ms.insert(it1, item);
break;
case 2:
cout<<"Enter value to be deleted: ";
cin>>item;
ms.erase(item);
break;
case 3:
cout<<"Enter element to find ";
cin>>item;
it = ms.find(item);
if (it != ms.end())
cout<<"Element found"<>item;
cout<
输出:
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 100
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 200
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 1
Enter value to be inserted: 300
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 2
Enter value to be deleted: 200
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 3
Enter element to find 100
Element found
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 4
Enter element to be counted: 100
100 appears 1 times.
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 5
Size of the Multiset: 2
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 6
Elements of the Multiset: 100 300
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 7
The First Element of the Multiset is 100
---------------------
Multiset Example
---------------------
1.Insert Number into the Multiset
2.Delete Element from the Multiset
3.Find Element in a Multiset
4.Count Elements with a specific key
5.Size of the Multiset
6.Display Multiset
7.First Element of the Multiset
8.Exit
Enter your Choice: 8
让我们看另一个示例来演示C++ Multiset:
#include
#include
#include
using namespace std;
int main()
{
// empty multiset container
multiset > ms1;
// insert elements in random order
ms1.insert(400);
ms1.insert(300);
ms1.insert(600);
ms1.insert(200);
ms1.insert(500);
ms1.insert(500); // 500 will be added again to the multiset unlike set
ms1.insert(100);
// printing multiset ms1
multiset :: iterator itr;
cout << "\nMarks of ms1 class Room: "< ms2(ms1.begin(), ms1.end());
// print all elements of the multiset ms2
cout << "\nThe Number of students in class Room after assigning Class Room students: "<::iterator msIt1, msIt2;
msIt1 = ms1.begin();
cout<< "\nHighest marks in ms1 Class Room: "<<*msIt1;
msIt2 = ms2.begin();
cout<< "\nHighest marks in ms2 Class Room: "<<*msIt2;
// remove all elements up to element with value 300 in ms2
cout << "\n\nms2 Class Room after removal of Students less than 300 marks:\n ";
ms2.erase(ms2.begin(), ms2.find(300));
for (itr = ms2.begin(); itr != ms2.end(); ++itr)
{
cout << " " << *itr;
}
// remove all elements with value 500 in ms2
int num;
num = ms2.erase(500);
cout << "\n\nms2.erase(500) : ";
cout << num << " removed \t" ;
for (itr = ms2.begin(); itr != ms2.end(); ++itr)
{
cout << " " << *itr;
}
cout << endl<
输出:
Marks of ms1 class Room:
600 500 500 400 300 200 100
The Number of students in class Room after assigning Class Room students:
100 200 300 400 500 500 600
Highest marks in ms1 Class Room: 600
Highest marks in ms2 Class Room: 100
ms2 Class Room after removal of Students less than 300 marks:
300 400 500 500 600
ms2.erase(500) : 2 removed 300 400 600
ms1.lower_bound(400) : 400
ms1.upper_bound(400) : 300
ms2.lower_bound(400) : 400
ms2.upper_bound(400) : 600
以下是多重集的所有成员函数的列表:
Functions | Description |
---|---|
(constructor) | Construct multiset |
(destructor) | Multiset destructor |
operator= | Copy elements of the multiset to another multiset. |
迭代器
Functions | Description |
---|---|
Begin | Returns an iterator pointing to the first element in the multiset. |
Cbegin | Returns a const iterator pointing to the first element in the multiset. |
End | Returns an iterator pointing to the past-end. |
cend | Returns a constant iterator pointing to the past-end. |
rbegin | Returns a reverse iterator pointing to the end. |
rend | Returns a reverse iterator pointing to the beginning. |
crbegin | Returns a constant reverse iterator pointing to the end. |
crend | Returns a constant reverse iterator pointing to the beginning. |
Functions | Description |
---|---|
empty | Returns true if multiset is empty. |
size | Returns the number of elements in the multiset. |
max_size | Returns the maximum size of the multiset. |
Functions | Description |
---|---|
insert | Insert element in the multiset. |
erase | Erase elements from the multiset. |
swap | Exchange the content of the multiset. |
clear | Delete all the elements of the multiset. |
emplace | Construct and insert the new elements into the multiset. |
emplace_hint | Construct and insert new elements into the multiset by hint. |
Functions | Description |
---|---|
key_comp | Return a copy of key comparison object. |
value_comp | Return a copy of value comparison object. |
Functions | Description |
---|---|
find | Search for an element with given key. |
count | Gets the number of elements matching with given key. |
lower_bound | Returns an iterator to lower bound. |
upper_bound | Returns an iterator to upper bound. |
equal_range | Returns the range of elements matches with given key. |
Functions | Description |
---|---|
get_allocator | Returns an allocator object that is used to construct the multiset. |
Functions | Description |
---|---|
operator== | Checks whether the two multisets are equal or not. |
operator!= | Checks whether the two multisets are equal or not. |
operator< | Checks whether the first multiset is less than other or not. |
operator<= | Checks whether the first multiset is less than or equal to other or not. |
operator> | Checks whether the first multiset is greater than other or not. |
operator>= | Checks whether the first multiset is greater than equal to other or not. |
swap() | Exchanges the element of two multisets. |