c++11 标准模板(STL)(std::unordered_map)(十一)
创始人
2024-06-02 20:07:57
0
定义于头文件 
template<

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

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

    template               class T,
              class Hash = std::hash,
              class KeyEqual = std::equal_to>
              using unordered_map = std::unordered_map                               std::pmr::polymorphic_allocator>>;

}
(2)(C++17 起)

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

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

 

桶接口

返回桶数

std::unordered_map::bucket_count

size_type bucket_count() const;

(C++11 起)

返回容器中的桶数。

参数

(无)

返回值

容器中的桶数。

复杂度

常数。

 

返回桶的最大数量

std::unordered_map::max_bucket_count

size_type max_bucket_count() const;

(C++11 起)

size_type max_bucket_count() const;

(C++11 起)

返回容器由于系统或库实现限制的能保有的最大桶数。

参数

(无)

返回值

最大桶数。

复杂度

常数。

 

返回在特定的桶中的元素数量

std::unordered_map::bucket_size

size_type bucket_size( size_type n ) const;

(C++11 起)

返回下标为 n 的桶中的元素数。

参数

n-要检验的桶的下标

返回值

n 中的元素数。

复杂度

与桶 n 的大小成线性。

 

返回带有特定键的桶

std::unordered_map::bucket

size_type bucket( const Key& key ) const;

(C++11 起)

返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。返回值仅对 bucket_count() 返回相同值的容器实例合法。

若 bucket_count() 为零则行为未定义。

参数

key-要检验的关键值

返回值

关键 key 的桶编号。

复杂度

常数。

 

调用示例

#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()
{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, std::to_string(n));};std::unordered_map unordered_map1;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的while (unordered_map1.size() < 6){unordered_map1.insert({generate()});//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。std::cout << "unordered_map1 bucket_count: " << unordered_map1.bucket_count() << std::endl;}std::cout << std::endl;std::cout << "unordered_map1:   ";std::copy(unordered_map1.begin(), unordered_map1.end(), std::ostream_iterator>(std::cout, " "));std::cout << std::endl;std::cout << std::endl;for (std::unordered_map::const_iterator cit =unordered_map1.cbegin(); cit != unordered_map1.end(); cit++){//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket = unordered_map1.bucket(cit->first);std::cout << "unordered_map1 bucket(" << cit->first << ") :    "<< bucket << "    ";//返回关键 key 的桶的下标。始终会在此桶中找到关键等于 key 的元素(若存在)。//返回值仅对 bucket_count() 返回相同值的容器实例合法。size_t bucket_size = unordered_map1.bucket_size(bucket);std::cout << "bucket_size(" << bucket << "):    " << bucket_size << std::endl;}std::cout << std::endl;//返回容器由于系统或库实现限制的能保有的最大桶数。std::unordered_map unordered_map_c;std::cout << "unordered_map max_bucket_count:           "<< unordered_map_c.max_bucket_count() << std::endl;std::unordered_map unordered_map_i;std::cout << "unordered_map max_bucket_count:             "<< unordered_map_i.max_bucket_count() << std::endl;std::unordered_map unordered_map_ui8;std::cout << "unordered_map max_bucket_count:     "<< unordered_map_ui8.max_bucket_count() << std::endl;std::unordered_map unordered_map_ui16;std::cout << "unordered_map max_bucket_count:   "<< unordered_map_ui16.max_bucket_count() << std::endl;std::unordered_map unordered_map_ui32;std::cout << "unordered_map max_bucket_count:   "<< unordered_map_ui32.max_bucket_count() << std::endl;std::unordered_map unordered_map_ui64;std::cout << "unordered_map max_bucket_count:   "<< unordered_map_ui64.max_bucket_count() << std::endl;std::unordered_map unordered_map_s;std::cout << "unordered_map max_bucket_count:         "<< unordered_map_s.max_bucket_count() << std::endl;std::unordered_map unordered_map_d;std::cout << "unordered_map max_bucket_count:       "<< unordered_map_d.max_bucket_count() << std::endl;std::unordered_map unordered_map_f;std::cout << "unordered_map max_bucket_count:         "<< unordered_map_f.max_bucket_count() << std::endl;std::unordered_map unordered_map_l;std::cout << "unordered_map max_bucket_count:           "<< unordered_map_l.max_bucket_count() << std::endl;std::unordered_map unordered_map_ll;std::cout << "unordered_map max_bucket_count: "<< unordered_map_ll.max_bucket_count() << std::endl;std::unordered_map unordered_map_str;std::cout << "unordered_map max_bucket_count:       "<< unordered_map_str.max_bucket_count() << std::endl;std::unordered_map unordered_map_Cell;std::cout << "std::unordered_map max_bucket_count: """ << unordered_map_Cell.max_bucket_count() << std::endl;return 0;
}

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...