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>::insert

iterator insert( const value_type& value );

(1) (C++11 起)

iterator insert( value_type&& value );

(1) (C++17 起)

template< class P >
iterator insert( P&& value );

(2) (C++11 起)

iterator insert( const_iterator hint, const value_type& value );

(3) (C++11 起)

iterator insert( const_iterator hint, value_type&& value );

(3) (C++17 起)

template< class P >
iterator insert( const_iterator hint, P&& value );

(4) (C++11 起)

template< class InputIt >
void insert( InputIt first, InputIt last );

(5) (C++11 起)

void insert( std::initializer_list<value_type> ilist );

(6) (C++11 起)

iterator insert(node_type&& nh);

(7) (C++17 起)

iterator insert(const_iterator hint, node_type&& nh);

(8) (C++17 起)

插入元素到容器中。

1-2) 插入 value 。重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。

5) 插入来自范围 [first, last) 的元素。

6) 插入来自 initializer_list ilist 的元素。

7) 若 nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器并返回指向被插入元素的迭代器。。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

8) 若 nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,并返回指向拥有等于 nh.key() 的关键的元素的迭代器元素被插入到尽可能接近 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

若因插入发生重哈希,则所有迭代器都被非法化。否则迭代器不受影响。引用不受影响。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)

参数

hint - 迭代器,用作插入内容位置的建议
value - 要插入的元素值
first, last - 要插入的元素范围
ilist - 插入值来源的 initializer_list
nh - 兼容的结点把柄
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-4) 返回指向被插入元素的迭代器。

5-6) (无)

7,8) 若 nh 为则为尾迭代器,否则为指向被插入元素的迭代器。

异常

1-4) 若任何操作抛出异常,则插入无效果。

本节未完成
原因:情况 5-6

复杂度

1-4) 平均情况: O(1) ,最坏情况 O(size())

5-6) 平均情况: O(N) ,其中 N 是要插入的元素数。最坏情况: O(N*size()+N)

7-8) 平均情况: O(1) ,最坏情况 O(size())

调用示例

#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++)
    {
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap1.insert(generate());
        std::cout << "unordered_multimap1 insert : " << *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++)
    {
        std::pair<Cell, string> cell = generate();
        //若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。移动语义
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap2.insert(std::move(cell));
        std::cout << "unordered_multimap2 insert : " << *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;


    std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap3;
    for (size_t index = 0; index < 5; index++)
    {
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap3.insert(unordered_multimap3.cbegin(), generate());
        std::cout << "unordered_multimap3 insert : " << *iit  << std::endl;
    }
    std::cout << "unordered_multimap3:   ";
    std::copy(unordered_multimap3.begin(), unordered_multimap3.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_multimap4;
    for (size_t index = 0; index < 5; index++)
    {
        std::pair<Cell, string> cell = generate();
        //3-4) 插入 value ,以 hint 为应当开始搜索的位置的非强制建议。移动语义
        std::unordered_multimap<Cell, string, CHash, CEqual>::iterator iit
            = unordered_multimap4.insert(unordered_multimap4.cbegin(), std::move(cell));
        std::cout << "unordered_multimap4 insert : " << *iit  << std::endl;
    }
    std::cout << "unordered_multimap4:   ";
    std::copy(unordered_multimap4.begin(), unordered_multimap4.end(),
              std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::vector<std::pair<Cell, string>> vector1(5);
    std::generate(vector1.begin(), vector1.end(), generate);
    std::cout << "vector1:          ";
    std::copy(vector1.begin(), vector1.end(),
              std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::unordered_multimap<Cell, string, CHash, CEqual> unordered_multimap5;
    //5) 插入来自范围 [first, last) 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_multimap5.insert(vector1.begin(), vector1.end());
    std::cout << "unordered_multimap5:   ";
    std::copy(unordered_multimap5.begin(), unordered_multimap5.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_multimap6;
    //6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的
    unordered_multimap6.insert({generate(), generate(), generate(), generate(), generate()});
    std::cout << "unordered_multimap6:   ";
    std::copy(unordered_multimap6.begin(), unordered_multimap4.end(),
              std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    return 0;
}

猜你喜欢

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