c++11 标准模板(STL)(std::unordered_map)(十二)
创始人
2024-06-03 06:23:11
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::begin(size_type), 
std::unordered_map::cbegin(size_type)

local_iterator begin( size_type n );

(C++11 起)

const_local_iterator begin( size_type n ) const;

(C++11 起)

const_local_iterator cbegin( size_type n ) const;

(C++11 起)

 返回指向下标为 n 的桶首元素的迭代器。

参数

n-要访问的桶的下标

返回值

指向首元素的迭代器。

复杂度

常数。

返回一个迭代器,指向指定的桶的末尾

std::unordered_map::end(size_type), 
std::unordered_map::cend(size_type)

local_iterator end( size_type n );

(C++11 起)

const_local_iterator end( size_type n ) const;

(C++11 起)

const_local_iterator cend( size_type n ) const;

(C++11 起)

 返回后随下标为 n 的桶的最后元素的元素的迭代器。此元素表现为占位符,试图访问它会导致未定义行为。

参数

n-要访问的桶的下标

返回值

指向后随最后元素的元素的迭代器。

复杂度

常数

调用示例

#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() < 5){unordered_map1.insert({generate()});}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 << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_map::const_local_iterator clit= unordered_map1.begin(bucket);std::cout << "const_local_iterator begin(" << bucket << "):    " << *clit << 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 << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_map::local_iterator clit =unordered_map1.begin(bucket);std::cout << "local_iterator begin(" << bucket << "):    " << *clit << 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 << "    ";//返回指向下标为 n 的桶首元素的迭代器。std::unordered_map::const_local_iterator clit =unordered_map1.cbegin(bucket);std::cout << "const_local_iterator cbegin(" << bucket << "):    " << *clit << std::endl;}return 0;
}

输出

 

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...