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

定义于头文件 
template<

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

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

    template               class Hash = std::hash,
              class Pred = std::equal_to>
    using unordered_multimap = std::unordered_multimap                                    std::pmr::polymorphic_allocator>>;

}
(2) (C++17 起)

 

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

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

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

成员函数

赋值给容器

std::unordered_multimap::operator=

unordered_multimap& operator=( const unordered_multimap& other );

(1) (C++11 起)

unordered_multimap& operator=( unordered_multimap&& other );

(2) (C++11 起)
(C++17 前)

unordered_multimap& operator=( unordered_multimap&& other ) noexcept(/* see below */);

(C++17 起)

unordered_multimap& operator=( std::initializer_list ilist );

(3) (C++11 起)

 

替换容器内容。

1) 复制赋值运算符。以 other 的副本替换内容。若 std::allocator_traits::propagate_on_container_copy_assignment::value 为 true ,则以源分配器的副本替换目标分配器。若源分配器与目标分配器不比较相等,则用目标( *this )分配器销毁内存,然后在复制元素前用 other 的分配器分配。 (C++11 起).、

2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。之后 other 在合法但未指定的状态。若 std::allocator_traits::propagate_on_container_move_assignment::value 为 true ,则用源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能取走源内存的所有权,而必须单独移动赋值逐个元素,用自己的分配器按需分配额外的内存。任何情况下,原先在 *this 中的元素要么被销毁,要么以逐元素移动赋值替换。

3) 以 initializer_list ilist 所标识者替换内容。

参数

other - 用作数据源的另一容器
ilist - 用作数据源的 initializer_list

返回值

*this

复杂度

1) 与 *thisother 的大小成线性。

2) 与 *this 的大小成线性,除非分配器不比较相等且不传播,该情况下与 *thisother 的大小成线性。

3) 与 *thisilist 的大小成线性。

异常

2)noexcept 规定:  noexcept(std::allocator_traits::is_always_equal::value

&& std::is_nothrow_move_assignable::value

&& std::is_nothrow_move_assignable::value)
(C++17 起)

注意

容器移动赋值(重载 (2) )后,除非不兼容的分配器强制逐元素赋值,否则指向 other 的引用、指针和迭代器(除了尾迭代器)都保持合法,不过指代的元素现在在 *this 中。当前标准通过 §23.2.1[container.requirements.general]/12 中的总括陈述保证这点,而 LWG 2321 下正在考虑更直接的保证。

返回相关的分配器

std::unordered_multimap::get_allocator

allocator_type get_allocator() const;

(C++11 起)

返回与容器关联的分配器。

参数

(无)

返回值

关联的分配器。

复杂度

常数。

 

调用示例

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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 &pCell)
{
    os << pCell.first << "-" << pCell.second;
    return os;
}

struct CHash
{
    size_t operator()(const Cell& cell) const
    {
        size_t thash = std::hash()(cell.x) | std::hash()(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, std::to_string(n));
    };

    std::unordered_multimap unordered_multimap1;
    while (unordered_multimap1.size() < 5)
    {
        unordered_multimap1.insert(generate());
    }
    std::cout << "unordered_multimap1:   ";
    std::copy(unordered_multimap1.begin(), unordered_multimap1.end(),
              std::ostream_iterator>(std::cout, " "));
    std::cout << std::endl;

    //1) 复制赋值运算符。以 other 的副本替换内容。
    std::unordered_multimap unordered_multimap2 =  unordered_multimap1;
    std::cout << "unordered_multimap2:   ";
    std::copy(unordered_multimap2.begin(), unordered_multimap2.end(),
              std::ostream_iterator>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    std::unordered_multimap unordered_multimap3;
    while (unordered_multimap3.size() < 5)
    {
        unordered_multimap3.insert(generate());
    }
    std::cout << "unordered_multimap3:   ";
    std::copy(unordered_multimap3.begin(), unordered_multimap3.end(),
              std::ostream_iterator>(std::cout, " "));
    std::cout << std::endl;

    //2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。
    std::unordered_multimap unordered_multimap4 =
        std::move(unordered_multimap3);
    std::cout << "unordered_multimap4:   ";
    std::copy(unordered_multimap4.begin(), unordered_multimap4.end(),
              std::ostream_iterator>(std::cout, " "));
    std::cout << std::endl;
    std::cout << "unordered_multimap3 is empty " << unordered_multimap3.empty()
              << "  bucket_count:   " << unordered_multimap3.bucket_count() << std::endl;
    std::cout << std::endl;


    //3) 以 initializer_list ilist 所标识者替换内容。
    std::unordered_multimap unordered_multimap5 =
    {generate(), generate(), generate(), generate(), generate(), generate()};
    std::cout << "unordered_multimap5:   ";
    std::copy(unordered_multimap5.begin(), unordered_multimap5.end(),
              std::ostream_iterator>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

输出

c++11 标准模板(STL)(std::unordered_multimap)(三)_第1张图片

 

你可能感兴趣的:(#,c++,哈希算法,无序关联容器,multimap,赋值语句)