c++11 标准模板(STL)(std::unordered_map)(二)

定义于头文件 <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_map;
(1)(C++11 起)
namespace pmr {

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

}
(2)(C++17 起)

 unordered_map关联容器,含有带唯一键的键-值 pair 。搜索、插入和元素移除拥有平均常数时间复杂度。

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

成员函数

构造 unordered_map

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::unordered_map
unordered_map() : unordered_map( size_type(/*implementation-defined*/) ) {}

explicit unordered_map( size_type bucket_count,
                        const Hash& hash = Hash(),
                        const key_equal& equal = key_equal(),

                        const Allocator& alloc = Allocator() );
(1)(C++11 起)
unordered_map( size_type bucket_count,

               const Allocator& alloc )
              : unordered_map(bucket_count, Hash(), key_equal(), alloc) {}
unordered_map( size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )

              : unordered_map(bucket_count, hash, key_equal(), alloc) {}
(1)(C++14 起)

explicit unordered_map( const Allocator& alloc );

(1)(C++11 起)
template< class InputIt >

unordered_map( InputIt first, InputIt last,
               size_type bucket_count = /*implementation-defined*/,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(2)(C++11 起)
template< class InputIt >

unordered_map( InputIt first, InputIt last,
               size_type bucket_count,
               const Allocator& alloc )
              : unordered_map(first, last,

                  bucket_count, Hash(), key_equal(), alloc) {}
(2)(C++14 起)
template< class InputIt >

unordered_map( InputIt first, InputIt last,
               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
              : unordered_map(first, last,

                  bucket_count, hash, key_equal(), alloc) {}
(2)(C++14 起)

unordered_map( const unordered_map& other );

(3)(C++11 起)

unordered_map( const unordered_map& other, const Allocator& alloc );

(3)(C++11 起)

unordered_map( unordered_map&& other );

(4)(C++11 起)

unordered_map( unordered_map&& other, const Allocator& alloc );

(4)(C++11 起)
unordered_map( std::initializer_list<value_type> init,

               size_type bucket_count = /*implementation-defined*/,
               const Hash& hash = Hash(),
               const key_equal& equal = key_equal(),

               const Allocator& alloc = Allocator() );
(5)(C++11 起)
unordered_map( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Allocator& alloc )
              : unordered_map(init, bucket_count,

                  Hash(), key_equal(), alloc) {}
(5)(C++14 起)
unordered_map( std::initializer_list<value_type> init,

               size_type bucket_count,
               const Hash& hash,
               const Allocator& alloc )
              : unordered_map(init, bucket_count,

                  hash, key_equal(), alloc) {}
(5)(C++14 起)

 

从各种数据源构造新容器。可选的以用户提供的 bucket_count 为用于创建的最小桶数,以 hash 为哈希函数,以 equal 为比较关键的函数,和以 alloc 为分配器。

1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。

2) 构造拥有范围 [first, last) 的内容的容器。设置 max_load_factor() 为 1.0 。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的(待决的 LWG2844 )。

3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。若不提供 alloc ,则通过调用 std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()) 获得分配器。

4) 移动构造函数。用移动语义构造拥有 other 内容的容器。若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。

5) 构造拥有 initializer_list init 内容的容器,同 unordered_map(init.begin(), init.end()) 。

参数

alloc-用于此容器所有内存分配器的分配器
bucket_count-初始化时用的最小桶数。若不指定,则使用实现定义的默认值
hash-要用的哈希函数
equal-用于此容器所有关键比较的比较函数
first, last-复制元素来源的范围
other-用作源以初始化容器元素的另一容器
init-用以初始化容器元素的 initializer_list
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

复杂度

1) 常数

2) 平均情况与 firstlast 间的距离成线性,最坏情况成平方。

3) 与 other 的大小成线性。

4) 常数。若给定 alloc 且 alloc != other.get_allocator() 则为线性。

5) 平均情况与 init 的大小成线性,最坏情况成平方。

异常

Allocator::allocate 的调用可能抛出。

注意

在容器移动构造(重载 (4) )后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 2321 正在考虑更严格的保证。

析构 unordered_map

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::~unordered_map

~unordered_map();

(C++11 起)

销毁容器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器大小成线性。

调用示例

 

#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()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

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

    //1) 构造空容器。设置 max_load_factor() 为 1.0 。对于默认构造函数,桶数是实现定义的。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;
    std::cout << "unordered_map1 is empty " << unordered_map1.empty()
              << "  bucket_count:   " << unordered_map1.bucket_count() << std::endl;

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map6(6);
    std::cout << "unordered_map6 is empty " << unordered_map6.empty()
              << "  bucket_count:   " << unordered_map6.bucket_count() << std::endl;
    std::cout << std::endl;

    std::vector<std::pair<Cell, string>> vector1(6);
    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;


    //2) 构造拥有范围 [first, last) 的内容的容器。
    //设置 max_load_factor() 为 1.0 。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map2(vector1.begin(), vector1.end());
    std::cout << "unordered_map2 size:  " << unordered_map2.size()
              << "  bucket_count:   " << unordered_map2.bucket_count() << std::endl;
    std::cout << "unordered_map2:   ";
    std::copy(unordered_map2.begin(), unordered_map2.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map7(vector1.begin(), vector1.end(), 8);
    std::cout << "unordered_map7 size:  " << unordered_map7.size()
              << "  bucket_count:   " << unordered_map7.bucket_count() << std::endl;
    std::cout << "unordered_map7:   ";
    std::copy(unordered_map7.begin(), unordered_map7.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //3) 复制构造函数。构造拥有 other 内容副本的容器,一同复制加载因子、谓词和哈希函数。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map3(unordered_map2);
    std::cout << "unordered_map3 size:  " << unordered_map3.size()
              << "  bucket_count:   " << unordered_map3.bucket_count() << std::endl;
    std::cout << "unordered_map3:   ";
    std::copy(unordered_map3.begin(), unordered_map3.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //4) 移动构造函数。用移动语义构造拥有 other 内容的容器。
    //若不提供 alloc ,则通过从属于 other 的分配器移动构造获得分配器。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map4(std::move(unordered_map2));
    std::cout << "unordered_map4:   ";
    std::copy(unordered_map4.begin(), unordered_map4.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;


    //5) 构造拥有 initializer_list init 内容的容器,同 unordered_map(init.begin(), init.end()) 。
    std::unordered_map<Cell, string, CHash, CEqual> unordered_map5
    {generate(), generate(), generate(), generate(), generate(), generate()};
    std::cout << "unordered_map5 size:  " << unordered_map5.size()
              << "  bucket_count:   " << unordered_map5.bucket_count() << std::endl;
    std::cout << "unordered_map5:   ";
    std::copy(unordered_map5.begin(), unordered_map5.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;

    std::unordered_map<Cell, string, CHash, CEqual> unordered_map8
    ({generate(), generate(), generate(), generate(), generate(), generate()}, 8);
    std::cout << "unordered_map8 size:  " << unordered_map8.size()
              << "  bucket_count:   " << unordered_map8.bucket_count() << std::endl;
    std::cout << "unordered_map8:   ";
    std::copy(unordered_map8.begin(), unordered_map8.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

输出

 


http://www.niftyadmin.cn/n/119085.html

相关文章

关于安卓的一些残缺笔记

安卓笔记Android应用项目的开发过程Android的调试Android项目文档结构Intent的显式/隐式调用Activity的生命周期1个Activity界面涉及到生命周期的情况2个Activity界面涉及到生命周期的情况Android布局的理论讲解Activity界面布局ContentProvider是如何实现数据共享Android整体架…

时间复杂度的计算

个人主页&#xff1a;平行线也会相交 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 平行线也会相交 原创 收录于专栏【数据结构初阶&#xff08;C实现&#xff09;】 文章目录123456789时间复杂度&#xff08;就是一个函数&#xff09;的计算&#xff0c;…

【fly-iot飞凡物联】(4):在linux系统上搭建arduino环境,可以使用离线包,导入到arduino上即可。

目录前言1&#xff0c;关于2&#xff0c;然后就可以找到ESP32&#xff0c;ESP8266的主版3&#xff0c;方法2&#xff0c;github下载&#xff0c;然后手动添加到ide中吧4&#xff0c;总结前言 本文的原文连接是: https://blog.csdn.net/freewebsys/article/details/108971807 未…

对于ThreadLocal的理解

对于ThreadLocal的理解什么是ThreadLocalThreadLocal的使用场景1.线程隔离2.跨函数传值正确使用感想什么是ThreadLocal 在Java的多线程并发执行的过程中&#xff0c;为了保证多个线程对变量的安全访问&#xff0c;可以将变量放到ThreadLocal类型的对象中&#xff0c;是变量在每…

泼辣修图Polarr5.11.4 版,让你的创意无限延伸

泼辣修图是一款非常实用的图片处理软件&#xff0c;它不仅拥有丰富的图片处理功能&#xff0c;而且还能够轻松地实现自定义操作。泼辣修图的操作界面非常简洁&#xff0c;功能也非常丰富&#xff0c;使用起来非常方便快捷。 泼辣修图拥有非常丰富的图片处理功能&#xff0c;包括…

华为机试题:HJ105 记负均正II(python)

文章目录&#xff08;1&#xff09;题目描述&#xff08;2&#xff09;Python3实现&#xff08;3&#xff09;知识点详解1、input()&#xff1a;获取控制台&#xff08;任意形式&#xff09;的输入。输出均为字符串类型。1.1、input() 与 list(input()) 的区别、及其相互转换方…

MAX 10 10M50 FPGA(10M50DDF256I7G)10M50DDF484C8G/10M50DDF484I7G

MAX 10器件是单芯片、非易失性低成本可编程逻辑器件(pld)&#xff0c;用于集成最优的系统组件集。MAX 10设备的亮点包括&#xff1a;内部存储双配置闪存用户闪存即时支持集成模数转换器(adc)支持Nios II单芯片软核处理器该器件设备是系统管理、I/O扩展、通信控制平面、工业、汽…

王道计算机组成原理课代表 - 考研计算机 第七章 输入输出系统 究极精华总结笔记

本篇博客是考研期间学习王道课程 传送门 的笔记&#xff0c;以及一整年里对 计算机组成 知识点的理解的总结。希望对新一届的计算机考研人提供帮助&#xff01;&#xff01;&#xff01; 关于对 “输入输出系统” 章节知识点总结的十分全面&#xff0c;涵括了《计算机组成原理》…