c++11 标准模板(STL)(std::unordered_multimap)(七)

定义于头文件 <unordered_map>
template<

    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >

> class unordered_multimap;
(1) (C++11 起)
namespace pmr {

    template <class Key, class T,
              class Hash = std::hash<Key>,
              class Pred = std::equal_to<Key>>
    using unordered_multimap = std::unordered_multimap<Key, T, Hash, Pred,
                                   std::pmr::polymorphic_allocator<std::pair<const Key,T>>>;

}
(2) (C++17 起)

 unordered_multimap 是无序关联容器,支持等价的关键(一个 unordered_multimap 可含有每个关键值的多个副本)和将关键与另一类型的值关联。 unordered_multimap 类支持向前迭代器。搜索、插入和移除拥有平均常数时间复杂度。

元素在内部不以任何特定顺序排序,而是组织到桶中。元素被放进哪个桶完全依赖于其关键的哈希。这允许到单独元素的快速访问,因为哈希一旦计算,则它指代元素被放进的准确的桶。

不要求此容器的迭代顺序稳定(故例如 std::equal 不能用于比较二个 std::unordered_multimap ),除了关键比较等价(以 key_eq() 为比较器比较相等)的每组元素在迭代顺序中组成相接的子范围,它亦可用 equal_range() 访问。

修改器

原位构造元素

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::emplace

template< class... Args >
iterator emplace( Args&&... args );

(C++11 起)

 插入以给定的 args 原位构造的新元素到容器。

细心地使用 emplace 允许在构造新元素的同时避免不必要的复制或移动操作。 准确地以与提供给 emplace 者相同的参数,通过 std::forward<Args>(args)... 转发调用新元素(即 std::pair<const Key, T> )的构造函数。


若因插入发生重哈希,则所有迭代器都被非法化。否则迭代器不受影响。引用不被非法化。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。

参数

args - 要转发给元素构造函数的参数

返回值

指向被插入元素的迭代器。

异常

若任何操作抛出异常,则此函数无效果。

复杂度

平均为均摊常数,最坏情况与容器大小成线性。

使用提示原位构造元素

std::unordered_multimap<Key,T,Hash,KeyEqual,Allocator>::emplace_hint

template <class... Args>
iterator emplace_hint( const_iterator hint, Args&&... args );

(C++11 起)

 插入新元素到容器,以 hint 为应当放置元素位置的建议。原位构造元素,即不进行复制或移动操作。

准确地与以提供给元素者相同的参数,以 std::forward<Args>(args)... 转发调用元素类型( value_type 即 std::pair<const Key, T> )的构造函数。

若因插入发生重哈希,则所有迭代器都被非法化。否则迭代器不受影响。引用不被非法化。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。

参数

hint - 迭代器,用作插入新元素位置的建议
args - 转发给元素构造函数的参数

返回值

返回指向新插入元素的迭代器。

异常

若任何操作抛出异常,则此函数无效果(强异常保证)。

复杂度

平均为均摊常数,最坏情况下与容器大小成线性。

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <unordered_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<Cell, string> &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}

struct CHash
{
    size_t operator()(const Cell& cell) const
    {
        size_t thash = std::hash<int>()(cell.x) | std::hash<int>()(cell.y);
//        std::cout << "CHash: " << thash << std::endl;
        return thash;
    }
};

struct CEqual
{
    bool operator()(const Cell &a, const Cell &b) const
    {
        return a.x == b.x && a.y == b.y;
    }
};

int main()
{
//    auto generate = []()
//    {
//        int n = std::rand() % 10 + 110;
//        Cell cell{n, n};
//        return std::pair<Cell, string>(cell, std::to_string(n));
//    };

    std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap1;
    for (size_t index = 0; index < 5; index++)
    {
        int n = std::rand() % 10 + 110;
        //若容器中无拥有该关键的元素,则插入以给定的 args 原位构造的新元素到容器。
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap1.emplace(Cell{n, n}, std::to_string(n));
        std::cout << "unordered_multimap1 emplace : " << *iit << std::endl;
    }
    std::cout << "unordered_multimap1:   ";
    std::copy(unordered_multimap1.begin(), unordered_multimap1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap2;
    for (size_t index = 0; index < 5; index++)
    {
        int n = std::rand() % 10 + 110;
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap2.emplace_hint(unordered_multimap2.cbegin(),
                                               Cell{n, n}, std::to_string(n));
        std::cout << "unordered_multimap2 emplace_hint : " << *iit  << std::endl;
    }
    std::cout << "unordered_multimap2:   ";
    std::copy(unordered_multimap2.begin(), unordered_multimap2.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //将内容与 other 的交换。不在单个元素上调用任何移动、复制或交换操作。
    unordered_multimap2.swap(unordered_multimap1);
    std::cout << "after swap:   " << std::endl;
    std::cout << "unordered_multimap1:   ";
    std::copy(unordered_multimap1.begin(), unordered_multimap1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "unordered_multimap2:   ";
    std::copy(unordered_multimap2.begin(), unordered_multimap2.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/129868137