c++11 标准模板(STL)(std::unordered_map)(十三)
创始人
2025-05-28 12:54:49
定义于头文件 
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::load_factor

float load_factor() const;

(C++11 起)

 返回每个桶元素的平均数,即 size() 除以 bucket_count() 。

参数

(无)

返回值

每个桶元素的平均数。

复杂度

常数。

管理每个桶的平均元素数量的最大值

std::unordered_map::max_load_factor

float max_load_factor() const;

(1)(C++11 起)

void max_load_factor( float ml );

(2)(C++11 起)

 管理最大加载因子(每个桶的平均元素数)。若加载因子超出此阈值,则容器自动增加桶数。

1) 返回最大加载因子。

2) 设置最大加载因子为 ml

参数

ml-新设置的最大加载因子

返回值

1) 当前最大加载因子。

2) 无。

复杂度

常数

为至少为指定数量的桶预留存储空间。

这会重新生成哈希表。

std::unordered_map::rehash

void rehash( size_type count );

(C++11 起)

 设置桶数为 count 并重哈希容器,即考虑桶总数已改变,再把元素放到适当的桶中。若新的桶数使加载因子大于最大加载因子( count < size() / max_load_factor() ),则新桶数至少为 size() / max_load_factor() 。

参数

count-新的桶数

返回值

(无)

复杂度

平均与容器大小成线性,最坏情况成平方。

注意

rehash(0) 可用于强制无条件的重哈希,例如在通过临时增加 max_load_factor() 暂停自动重哈希之后。

为至少为指定数量的元素预留存储空间。

这会重新生成哈希表。

std::unordered_map::reserve

void reserve( size_type count );

(C++11 起)

 设置桶数为适应至少 count 个元素,而不超出最大加载因子所需的数,并重哈希容器,即考虑桶数已更改后将元素放进适合的桶。等效地调用 rehash(std::ceil(count / max_load_factor())) 。

参数

count-容器的新容量

返回值

(无)

复杂度

平均情况与容器大小成线性,最坏情况成平方。

调用示例

#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;while (unordered_map1.size() < 6){//返回每个桶元素的平均数,即 size() 除以 bucket_count() 。std::cout << "unordered_map1 load_factor :  " << unordered_map1.size()<< " / " << unordered_map1.bucket_count() << " = "<< unordered_map1.load_factor() << std::endl;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的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;std::unordered_map unordered_map2;size_t index = 1;while (unordered_map2.size() < 3){//管理最大加载因子(每个桶的平均元素数)。若加载因子超出此阈值,则容器自动增加桶数。//1) 返回最大加载因子。std::cout << "unordered_map2 max_load_factor    :  "<< unordered_map2.max_load_factor() << std::endl;//2) 设置最大加载因子为 ml 。unordered_map2.max_load_factor(index);std::cout << "unordered_map2 max_load_factor(" << index << ") :  "<< unordered_map2.max_load_factor() << std::endl;//返回每个桶元素的平均数,即 size() 除以 bucket_count() 。std::cout << "unordered_map2 load_factor        :  " << unordered_map2.size()<< " / " << unordered_map2.bucket_count() << " = "<< unordered_map2.load_factor() << std::endl;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的unordered_map2.insert({generate()});index++;}std::cout << std::endl;std::unordered_map unordered_map3;index = 1;while (unordered_map3.size() < 3){unordered_map3.max_load_factor(index);std::cout << "unordered_map3 max_load_factor(" << index << ") :  "<< unordered_map3.max_load_factor() << std::endl;//1) 返回最大加载因子。std::cout << "unordered_map3 max_load_factor    :  "<< unordered_map3.max_load_factor() << std::endl;//设置桶数为 count 并重哈希容器,即考虑桶总数已改变,再把元素放到适当的桶中unordered_map3.rehash(index);std::cout << "unordered_map3 rehash(" << index << ") " << std::endl;//返回每个桶元素的平均数,即 size() 除以 bucket_count() 。std::cout << "unordered_map3 load_factor        :  " << unordered_map3.size()<< " / " << unordered_map3.bucket_count() << " = "<< unordered_map3.load_factor() << std::endl;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的unordered_map3.insert({generate()});index++;}std::cout << std::endl;std::unordered_map unordered_map4;index = 1;while (unordered_map4.size() < 3){unordered_map4.max_load_factor(index);std::cout << "unordered_map4 max_load_factor(" << index << ") :  "<< unordered_map4.max_load_factor() << std::endl;//1) 返回最大加载因子。std::cout << "unordered_map4 max_load_factor    :  "<< unordered_map4.max_load_factor() << std::endl;//设置桶数为 count 并重哈希容器,即考虑桶总数已改变,再把元素放到适当的桶中unordered_map4.reserve(index);std::cout << "unordered_map4 reserve(" << index << ") " << std::endl;//返回每个桶元素的平均数,即 size() 除以 bucket_count() 。std::cout << "unordered_map4 load_factor        :  " << unordered_map3.size()<< " / " << unordered_map4.bucket_count() << " = "<< unordered_map4.load_factor() << std::endl;//6) 插入来自 initializer_list ilist 的元素。若范围中的多个元素拥有比较等价的关键,则插入哪个元素是未指定的unordered_map4.insert({generate()});index++;}return 0;
}

相关内容

热门资讯

云龙天池国家级自然保护区入选世... 转自:云南日报记者近日从云龙天池国家级自然保护区获悉,该保护区正式入选世界自然保护联盟绿色名录,成为...
芒果干里的“暖心账”​ 我 为 群 众 办 实 事我是楚雄彝族自治州楚雄市八角镇大麦地村委会泥期苴小组的鲁晓玲。天还没亮,新...
在亲戚借条上签名被判连带清偿责...   三湘都市报12月14日讯  欠钱逾期未还,双方公堂对簿,竟因借据上的“担保人”“连带担保人”起了...
学分能换“高级工”证书?湖南暂...   毕业就能拿到“高级工”的技能证书,实现“毕业即持证”,这是种什么样的体验?近日,安徽皖江工学院土...
长赣高铁湖南段首座隧道进洞施工     12月13日,位于浏阳市荷花街道和澄潭江镇的长赣高铁湖南段首座隧道——苏家庵隧道正式进洞。 ...