定义于头文件 <set>
| template<     class Key, | (1) | |
| namespace pmr {     template <class Key, class Compare = std::less<Key>> | (2) | (C++17 起) | 
std::multiset 是含有 Key 类型对象有序集的容器。不同于 set ,它允许多个关键拥有等价的值。用关键比较函数 Compare 进行排序。搜索、插入和移除操作拥有对数复杂度。
在标准库使用比较 (Compare) 概念的每处,都用描述于比较 (Compare) 的等价关系确定等价性。不精确地说,若二个对象 a 和 b 互不比较小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。
比较等价的元素顺序是插入顺序,而且不会更改。(C++11 起)
成员函数
构造函数
std::multiset<Key,Compare,Allocator>::multiset| multiset(); explicit multiset( const Compare& comp,const Allocator& alloc = Allocator() ); | (1) | |
| explicit multiset( const Allocator& alloc ); | (1) | (C++11 起) | 
| template< class InputIt > multiset( InputIt first, InputIt last, | (2) | |
| template< class InputIt > multiset( InputIt first, InputIt last,const Allocator& alloc ); | (C++14 起) | |
| multiset( const multiset& other ); | (3) | |
| multiset( const multiset& other, const Allocator& alloc ); | (3) | (C++11 起) | 
| multiset( multiset&& other ); | (4) | (C++11 起) | 
| multiset( multiset&& other, const Allocator& alloc ); | (4) | (C++11 起) | 
| multiset( std::initializer_list<value_type> init, const Compare& comp = Compare(),const Allocator& alloc = Allocator() ); | (5) | (C++11 起) | 
| multiset( std::initializer_list<value_type> init, | (C++14 起) | 
从各种数据源构造新容器,可选地使用用户提供的分配器 alloc 或比较函数对象 comp 。
1) 构造空容器。
2) 构造容器,使之拥有范围 [first, last) 的内容。
3) 复制构造函数。构造容器,使之拥有 other 的内容副本。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。
4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。若不提供 alloc ,则从属于 other 的分配器移动构造分配器
5) 构造容器,使之拥有 initializer_list init 的内容。
参数
| alloc | - | 用于此容器所有内存分配的分配器 | 
| comp | - | 用于所有关键比较的比较函数对象 | 
| first, last | - | 复制元素来源的范围 | 
| other | - | 要用作源以初始化容器元素的另一容器 | 
| init | - | 用以初始化容器元素的 initializer_list | 
| 类型要求 | ||
| - InputIt必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。 | ||
| - Compare必须满足比较 (Compare) 的要求。 | ||
| - Allocator必须满足分配器 (Allocator) 的要求。 | ||
复杂度
1) 常数。
2) N log(N) ,其中通常有 N = std::distance(first, last) ,若范围已为 value_comp() 所排序则与 N 成线性。
3) 与 other 的大小成线性。
4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。
5) N log(N) ,其中通常有 N = init.size()) ,若 init 已按照 value_comp() 排序则与 N 成线性 。
异常
对 Allocator::allocate 的调用可能抛出。
注意
在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。
析构函数
std::multiset<Key,Compare,Allocator>::~multiset| ~multiset(); | 
销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。
复杂度
与容器大小成线性。
调用示例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <set>
using namespace std;
using namespace std;
struct Cell
{
    int x;
    int y;
    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }
    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }
    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }
    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }
    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }
    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }
    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}
int main()
{
    std::cout << std::boolalpha;
    //1) 构造空容器。
    std::multiset<Cell> multiset1;
    std::cout << "multiset1 is empty " << multiset1.empty() << std::endl;
    std::cout << std::endl;
    std::vector<Cell> vector1{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    //2) 构造容器,使之拥有范围 [first, last) 的内容。
    std::multiset<Cell> multiset2(vector1.begin(), vector1.end());
    std::cout << "multiset2:    ";
    std::copy(multiset2.begin(), multiset2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell, std::greater<Cell>> multiset3(vector1.begin(), vector1.end());
    std::cout << "multiset3:    ";
    std::copy(multiset3.begin(), multiset3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    //3) 复制构造函数。构造容器,使之拥有 other 的内容副本。
    std::multiset<Cell> multiset4(multiset2);
    std::cout << "multiset4:    ";
    std::copy(multiset4.begin(), multiset4.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell, std::greater<Cell>> multiset5(multiset3);
    std::cout << "multiset5:    ";
    std::copy(multiset5.begin(), multiset5.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    //4) 移动构造函数。用移动语义构造容器,使之拥有 other 的内容。
    std::multiset<Cell> multiset6(std::move(multiset2));
    std::cout << "multiset6:    ";
    std::copy(multiset6.begin(), multiset6.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell, std::greater<Cell>> multiset7(std::move(multiset3));
    std::cout << "multiset7:    ";
    std::copy(multiset7.begin(), multiset7.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    //5) 构造容器,使之拥有 initializer_list init 的内容。
    std::multiset<Cell> multiset8{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    std::cout << "multiset8:    ";
    std::copy(multiset8.begin(), multiset8.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::multiset<Cell, std::greater<Cell>> multiset9{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    std::cout << "multiset9:    ";
    std::copy(multiset9.begin(), multiset9.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}
输出



















