当前位置: 首页 > news >正文

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 。搜索、插入和元素移除拥有平均常数时间复杂度。

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

成员函数

赋值给容器

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::operator=

unordered_map& operator=( const unordered_map& other );

(1)(C++11 起)

unordered_map& operator=( unordered_map&& other );

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

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

(C++17 起)

unordered_map& operator=( std::initializer_list<value_type> ilist );

(3)(C++11 起)

替换容器内容。

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

2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。之后 other 在合法但未指定的状态。若 std::allocator_traits<allocator_type>::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<Allocator>::is_always_equal::value

&& std::is_nothrow_move_assignable<Hash>::value

&& std::is_nothrow_move_assignable<Pred>::value)
(C++17 起)

注意

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

 

返回相关的分配器

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

allocator_type get_allocator() const;

(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));};std::unordered_map<Cell, string, CHash, CEqual> unordered_map1;while (unordered_map1.size() < 5){unordered_map1.insert(generate());}std::cout << "unordered_map1:   ";std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator<std::pair<Cell, string>>(std::cout, " "));std::cout << std::endl;//1) 复制赋值运算符。以 other 的副本替换内容。std::unordered_map<Cell, string, CHash, CEqual> unordered_map2 =  unordered_map1;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::cout << std::endl;std::unordered_map<Cell, string, CHash, CEqual> unordered_map3;while (unordered_map3.size() < 5){unordered_map3.insert(generate());}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;//2) 移动赋值运算符。用移动语义以 other 的内容替换内容(即从 other 移动 other 中的数据到此容器)。std::unordered_map<Cell, string, CHash, CEqual> unordered_map4 =  std::move(unordered_map3);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 << "unordered_map3 is empty " << unordered_map3.empty()<< "  bucket_count:   " << unordered_map3.bucket_count() << std::endl;std::cout << std::endl;//3) 以 initializer_list ilist 所标识者替换内容。std::unordered_map<Cell, string, CHash, CEqual> unordered_map5 ={generate(), generate(), generate(), generate(), generate(), generate()};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;return 0;
}

输出

 

http://www.lryc.cn/news/30819.html

相关文章:

  • OpenGL环境配置
  • SpringCloud之 Eureka注册中心
  • Linux入门篇-用户管理
  • G. Special Permutation(构造)
  • QML动态对象管理
  • cmake入门03 -自定义find外部库
  • Dubbo源码解析-——服务导出
  • vue+django+neo4j 基于知识图谱红楼梦问答系统
  • 2023年全国最新食品安全管理员精选真题及答案13
  • Keychron K7 Pro 轻薄矮轴机械键盘开箱体验
  • 加油站ai视觉识别系统 yolov7
  • 【电子学会】2022年12月图形化二级 -- 绘制风车
  • 【golang/go语言】Go语言代码实践——高复用、易扩展性代码训练
  • [数据结构与算法(严蔚敏 C语言第二版)]第1章 绪论(学习复习笔记)
  • 05_Pulsar的主要组件介绍与命令使用、名称空间、Pulsar的topic相关操作、Pulsar Topic(主题)相关操作_高级操作、
  • 我的终端怎么莫名卡死了?shell下ctrl+s的含义
  • 【Vue】Vue的简单介绍与基本使用
  • 网络知识篇
  • python 连接数据库
  • 一文讲明白一致性hash算法
  • Java分布式解决方案(一)
  • 设备树系统学习(二)设备树的节点和属性
  • 【数据结构】二叉树的基本操作中的一些易错点
  • 在线图书借阅网站( Python +Vue 实现)
  • 不平衡数据集的建模的技巧和策略
  • 3. 算法效率
  • 仪表放大器放大倍数分析-运算放大器
  • laravel8多模块、多应用和多应用路由
  • 【Java学习笔记】6.Java 变量类型
  • Promise对象状态属性 工作流程 Promise对象的几个属性