C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ STL multiset

multiset简介

multiset是 C++ STL的一部分(标准模板库)。多重集是类似于Set的关联容器,用于存储排序后的值(该值本身是T类型的键),但是与Set仅存储唯一键的Set不同, multiset可以具有重复的键 。默认情况下,它使用运算符比较键。
可以插入或删除multiset中元素的值,但不能更改(元素始终为const)。

语法

template < class T,                             // multiset::key_type/value_type
           class Compare = less<T>,        // multiset::key_compare/value_compare
           class Alloc = allocator<T>         // multiset::allocator_type
           > class multiset;

参数

T : 存储在容器多重集中的元素类型。
比较 : 一个比较类,它接受两个具有相同bool类型的参数并返回一个值。此参数是可选的,二进制谓词较少 是默认值。
Alloc : 用于定义存储分配模型的分配器对象的类型。

示例1

让我们看一个演示C++ Multiset的示例:
#include <iostream>
#include <set>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    multiset<int> ms;
    multiset<int>::iterator it, it1, msIt;
    int choice, item;   
    
    while (1)
    {
        cout<<"\n---------------------"<<endl;
        cout<<"Multiset Example"<<endl;
        cout<<"\n---------------------"<<endl;
        cout<<"1.Insert Number into the Multiset"<<endl;
        cout<<"2.Delete Element from the Multiset"<<endl;
        cout<<"3.Find Element in a Multiset"<<endl;
        cout<<"4.Count Elements with a specific key"<<endl;
        cout<<"5.Size of the Multiset"<<endl;
        cout<<"6.Display Multiset"<<endl;
        cout<<"7.First Element of the Multiset"<<endl;
        cout<<"8.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>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"<<endl;
            else
                cout<<"Element not found"<<endl;
            break;
        case 4:
            cout<<"Enter element to be counted: ";
            cin>>item;
            cout<<item<<" appears "<<ms.count(item)<<" times."<<endl;
            break;
        case 5:
            cout<<"Size of the Multiset: "<<ms.size()<<endl;
            break;
        case 6:
            cout<<"Elements of the Multiset:  ";
            for (it = ms.begin(); it != ms.end(); it++)
                cout<<*it<<"  ";
            cout<<endl;
            break;
        case 7:
        if(ms.empty())
        {
            cout<<"Multiset is empty";
        }
        else
        {
            msIt =  ms.begin();
                cout << "The First Element of the Multiset is " << *msIt << endl; 
        }
             break;
        case 8:
            exit(1);
            break;
        default:
            cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}
输出:
---------------------
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

示例2

让我们看另一个示例来演示C++ Multiset:
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
    // empty multiset container
    multiset <int, greater <int> > 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 <int> :: iterator itr;
    
    cout << "\nMarks of ms1 class Room: "<<endl;
    for (itr = ms1.begin(); itr != ms1.end(); ++itr)
    {
        cout << "  " << *itr;
    }
    cout << endl;
    
    // assigning the elements from ms1 to ms2
    multiset <int> 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: "<<endl;
    for (itr = ms2.begin(); itr != ms2.end(); ++itr)
    {
        cout << "  " << *itr;
    }
    cout << endl;
    
    // Find the highest element in multiset ms1 and ms2
    multiset<int>::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<<endl;
    //lower bound and upper bound for multiset ms1
    cout << "ms1.lower_bound(400) : " << *ms1.lower_bound(400) << endl;
    cout << "ms1.upper_bound(400) : " << *ms1.upper_bound(400) << endl;
    //lower bound and upper bound for multiset ms2
    cout << "ms2.lower_bound(400) : " << *ms2.lower_bound(400) << endl;
    cout << "ms2.upper_bound(400) : " << *ms2.upper_bound(400) << endl;
    
return 0;
}
输出:
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

成员函数

以下是multiset的所有成员函数的列表:

构造函数/析构函数

功能 说明
(构造函数) 构造多重集
(析构函数) multiset析构函数
operator= 将多重集的元素复制到另一个多重集。
迭代器
功能 说明
Begin 返回指向多重集中第一个元素的迭代器。
Cbegin 返回指向多重集中第一个元素的const迭代器。
End 返回指向末尾的迭代器。
cend 返回一个指向末尾的常量迭代器。
rbegin 返回指向结尾的反向迭代器。
rend 返回指向起点的反向迭代器。
crbegin 返回一个指向末尾的常量反向迭代器。
credit 返回指向起点的常量反向迭代器。

容量

功能 说明
empty 如果multiset为空,则返回true。
size 返回多重集中的元素数。
max_size 返回multiset的最大大小。

修饰符

功能 说明
insert 将元素插入multiset中。
erase 从multiset中擦除元素。
swap 交换multiset的内容。
clear 删除多重集的所有元素。
emplace 构造新元素并将其插入多重集。
emplace_hint 通过提示构造并将新元素插入到multiset中。

观察者

功能 说明
key_comp 返回键比较对象的副本。
value_comp 返回值比较对象的副本。

操作

功能 说明
find 搜索具有给定键的元素。
count 获取与给定键匹配的元素数。
lower_bound 将迭代器返回到下限。
upper_bound 将迭代器返回上限。
equal_range 返回与给定键匹配的元素的范围。

分配器

功能 说明
get_allocator 返回用于构造multiset的分配器对象。

非成员重载函数

功能 说明
operator== 检查两个多重集是否相等。
operator!= 检查两个多重集是否相等。
operator< 检查第一个多重集是否小于其他多重集。
operator<= 检查第一个多重集是否小于或等于另一个。
operator> 检查第一个多重集是否大于其他多重集。
operator>= 检查第一个多重集是否大于或等于另一个。
swap() 交换两个multiset的元素。


昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4