定义于头文件 <map>
| template<     class Key, | (1) | |
| namespace pmr {     template <class Key, class T, class Compare = std::less<Key>> | (2) | (C++17 起) | 
 multimap 是关联容器,含有关键-值 pair 的已排序列表,同时容许多个入口拥有同一关键。按照应用到关键的比较函数 Compare 排序。搜索、插入和移除操作拥有对数复杂度。
拥有等价关键的关键-值 pair 的顺序就是插入顺序,且不会更改。(C++11 起)
凡在标准库使用比较 (Compare) 概念出,都用描述于比较 (Compare) 上的等价关系确定等价性。不精确地说,若二个对象 a 和 b 互不小于对方: !comp(a, b) && !comp(b, a) ,则认为它们等价。
查找
返回匹配特定键的元素数量
std::multimap<Key,T,Compare,Allocator>::count| size_type count( const Key& key ) const; | (1) | |
| template< class K > | (2) | (C++14 起) | 
返回拥有关键比较等价于指定参数的元素数。
1) 返回拥有关键 key 的元素数。
2) 返回拥有关键比较等价于值 x 的元素数。此重载仅若有限定 id Compare::is_transparent 合法且指代一个类型才参与重载决议。这允许调用此函数而不构造 Key 的实例。
参数
| key | - | 要计量元素数的关键值 | 
| x | - | 要与关键比较的替用值 | 
返回值
拥有比较等价于 key 或 x 的关键的元素数。
复杂度
与容器大小成对数,加上与找到的元素数成线性。
寻找带有特定键的元素
std::multimap<Key,T,Compare,Allocator>::find| iterator find( const Key& key ); | (1) | |
| const_iterator find( const Key& key ) const; | (2) | |
| template< class K > iterator find( const K& x ); | (3) | (C++14 起) | 
| template< class K > const_iterator find( const K& x ) const; | (4) | (C++14 起) | 
1,2) 寻找键等于 key 的的元素。若容器中有数个拥有键 key 的元素,则可能返回任意一者。
3,4) 寻找键比较等价于值 x 的元素。此重载仅若若有限定 id Compare::is_transparent 合法并且指代类型才参与重载决议。允许调用此函数而无需构造 Key 的实例。
参数
| key | - | 要搜索的元素键值 | 
| x | - | 能通透地与键比较的任何类型值 | 
返回值
指向键等于 key 的元素的迭代器。若找不到这种元素,则返回尾后(见 end() )迭代器。
复杂度
与容器大小成对数。
返回匹配特定键的元素范围
std::multimap<Key,T,Compare,Allocator>::equal_range| std::pair<iterator,iterator> equal_range( const Key& key ); | (1) | |
| std::pair<const_iterator,const_iterator> equal_range( const Key& key ) const; | (2) | |
| template< class K > | (3) | (C++14 起) | 
| template< class K > | (4) | (C++14 起) | 
 返回容器中所有拥有给定关键的元素范围。范围以二个迭代器定义,一个指向首个不小于 key 的元素,另一个指向首个大于 key 的元素。首个迭代器可以换用 lower_bound() 获得,而第二迭代器可换用 upper_bound() 获得。
1,2) 比较关键与 key 。
3,4) 比较关键与值 x 。此重载仅若有限定 id Compare::is_transparent 合法且指代一个类型才参与重载决议。它们允许调用此函数而不构造 Key 的实例。
| 本节未完成 原因:解释为何更好 | 
参数
| key | - | 要比较元素的关键值 | 
| x | - | 能与 Key比较的替用值 | 
返回值
含一对定义所需范围的迭代器的 std::pair :第一个指向首个不小于 key 的元素,第二个指向首个大于 key 的元素。
若无元素不小于 key ,则将尾后(见 end() )迭代器作为第一元素返回。类似地,若无元素大于 key ,则将尾后迭代器作为第二元素返回。
| 因为  | (C++11 起) | 
复杂度
与容器大小成对数。
返回指向首个不小于给定键的元素的迭代器
std::multimap<Key,T,Compare,Allocator>::lower_bound| iterator lower_bound( const Key& key ); | (1) | |
| const_iterator lower_bound( const Key& key ) const; | (1) | |
| template< class K > | (2) | (C++14 起) | 
| template< class K > | (2) | (C++14 起) | 
 1) 返回指向首个不小于 key 的元素的迭代器。
2) 返回指向首个比较不小于值 x 的元素的迭代器。此重载仅若有限定 id Compare::is_transparent 合法并指代一个类型才参与重载决议。它们允许调用此函数而无需构造 Key 的实例。
参数
| key | - | 要与元素比较的关键值 | 
| x | - | 能与 Key比较的替用值 | 
返回值
指向首个不小于 key 的元素的迭代器。若找不到这种元素,则返回尾后迭代器(见 end() )。
复杂度
与容器大小成对数。
返回指向首个大于给定键的元素的迭代器
std::multimap<Key,T,Compare,Allocator>::upper_bound| iterator upper_bound( const Key& key ); | (1) | |
| const_iterator upper_bound( const Key& key ) const; | (1) | |
| template< class K > | (2) | (C++14 起) | 
| template< class K > | (2) | (C++14 起) | 
 1) 返回指向首个大于 key 的元素的迭代器。
2) 返回指向首个比较大于值 x 的元素的迭代器。此重载仅若有限定 id Compare::is_transparent 合法并指代一个类型才参与重载决议。这允许调用此函数而无需构造 Key 的实例。
参数
| key | - | 与元素比较的关键值 | 
| x | - | 能与 Key比较的替用值 | 
返回值
指向首个大于 key 的元素的迭代器。若找不到这种元素,则返回尾后(见 end() )迭代器。
复杂度
与容器大小成对数。
调用示例
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <time.h>
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;
    }
};
struct myCompare
{
    bool operator()(const int &a, const int &b)
    {
        return a < b;
    }
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}
std::ostream &operator<<(std::ostream &os, const std::pair<const int, Cell> &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}
int main()
{
    std::cout << std::boolalpha;
    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));
    auto genKey = []()
    {
        return std::rand() % 10 + 100;
    };
    auto generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };
    std::multimap<int, Cell> multimap1;
    for (size_t index = 0; index < 6; index++)
    {
        //插入以给定的 args 原位构造的新元素到容器。
        multimap1.emplace(genKey(), generate());
    }
    std::cout << "multimap1:    ";
    std::copy(multimap1.begin(), multimap1.end(), std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    for (std::multimap<int, Cell>::const_iterator it = multimap1.cbegin(); it != multimap1.end(); it++)
    {
        std::cout << "multimap1 key ( " << it->first << " ) ";
        //返回拥有关键比较等价于指定参数的元素数。
        std::cout << "count :   " << multimap1.count(it->first);
        std::cout << std::endl;
    }
    std::cout << std::endl;
    for (std::multimap<int, Cell>::const_iterator it = multimap1.cbegin(); it != multimap1.end(); it++)
    {
        std::cout << "multimap1 find key ( " << it->first << " ) ";
        //1,2) 寻找键等于 key 的的元素。若容器中有数个拥有键 key 的元素,则可能返回任意一者。
        std::multimap<int, Cell>::const_iterator fit = multimap1.find(it->first);
        std::cout << "value :   " << fit->second;
        std::cout << std::endl;
    }
    std::cout << std::endl;
    for (std::multimap<int, Cell>::const_iterator it = multimap1.cbegin(); it != multimap1.end(); it++)
    {
        //返回容器中所有拥有给定关键的元素范围。
        //范围以二个迭代器定义,一个指向首个不小于 key 的元素,另一个指向首个大于 key 的元素。
        std::pair<std::multimap<int, Cell>::iterator, std::multimap<int, Cell>::iterator> pit =
            multimap1.equal_range(it->first);
        std::cout << "multimap1 equal_range key ( " << it->first << " ) ";
        std::copy(pit.first, pit.second, std::ostream_iterator<std::pair<const int, Cell>>(std::cout, " "));
        std::cout << std::endl;
    }
    std::cout << std::endl;
    for (std::multimap<int, Cell>::const_iterator it = multimap1.cbegin(); it != multimap1.end(); it++)
    {
        //1) 返回指向首个不小于 key 的元素的迭代器。
        std::multimap<int, Cell>::const_iterator lit = multimap1.lower_bound(it->first);
        std::cout << "multimap1 lower_bound key ( " << lit->first << " ) ";
        std::cout << "value :   " << lit->second;
        std::cout << std::endl;
    }
    std::cout << std::endl;
    for (std::multimap<int, Cell>::const_iterator it = multimap1.cbegin(); it != multimap1.end(); it++)
    {
        //1) 返回指向首个大于 key 的元素的迭代器。
        std::multimap<int, Cell>::const_iterator uit = multimap1.upper_bound(it->first);
        if (uit == multimap1.end())
        {
            continue;
        }
        std::cout << "multimap1 upper_bound key ( " << uit->first << " ) ";
        std::cout << "value :   " << uit->second;
        std::cout << std::endl;
    }
    return 0;
}输出




















