c++11 标准模板(STL)(std::unordered_map)(十)
创始人
2024-06-02 11:14:40
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::count

size_type count( const Key& key ) const;

(1)(C++11 起)

template< class K >
size_type count( const K& x ) const;

(2)(C++20 起)

 1) 返回拥有比较等于指定参数 key 的关键的元素数,因为此容器不允许重复故为 1 或 0 。

2) 返回键比较等价于指定参数 x 的元素数。此重载仅若有限定 Hash::transparent_key_equal 合法并指代类型才参与重载决议。这假设能用 KKey 类型一起调用这种 Hash ,还有其 key_equal 是通透的,进而允许不用构造 Key 的实例就调用此函数。

参数

key-要计量等价元素数的键值
x-能通透地与键比较的任何类型值

返回值

1) 拥有关键 key 的元素数,即 1 或 0 。

2) 键比较等价于 x 的元素数。

复杂度

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

寻找带有特定键的元素

std::unordered_map::find

iterator find( const Key& key );

(1)

const_iterator find( const Key& key ) const;

(2)

template< class K > iterator find( const K& x );

(3)(C++20 起)

template< class K > const_iterator find( const K& x ) const;

(4)(C++20 起)

 1,2) 寻找键等于 key 的的元素。

3,4) 寻找键比较等价于值 x 的元素。此重载仅若有限定 Hash::transparent_key_equal 合法并指代类型才参与重载决议。这假设能用 KKey 类型一起调用这种 Hash ,还有其 key_equal 是通透的,进而允许不用构造 Key 的实例就调用此函数。

参数

key-要搜索的元素键值
x-能通透地与键比较的任何类型值

返回值

指向键等于 key 的元素的迭代器。若找不到这种元素,则返回尾后(见 end() )迭代器。

复杂度

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

返回匹配特定键的元素范围

std::unordered_map::equal_range

std::pair equal_range( const Key& key );

(1)(C++11 起)

std::pair equal_range( const Key& key ) const;

(2)(C++11 起)

template< class K >
std::pair equal_range( const K& x );

(3)(C++20 起)

template< class K >
std::pair equal_range( const K& x ) const;

(4)(C++20 起)

 1,2) 返回容器中所有键等于 key 的元素范围。范围以二个迭代器定义,第一个指向所需范围的首元素,而第二个指向范围的尾后一位元素。

3,4) 返回含有容器中所有键等价于 x 的元素的范围。此重载仅若有限定 Hash::transparent_key_equal 合法并指代类型才参与重载决议。这假设能用 KKey 类型一起调用这种 Hash ,还有其 key_equal 是通透的,进而允许不用构造 Key 的实例就调用此函数。

参数

key-要与元素比较的键值
x-任何能与键通透比较的类型的值

返回值

含一对定义所需范围的迭代器的 std::pair 。若无这种元素,则将尾后(见 end() )迭代器作为 pair 的两个元素返回。

复杂度

平均情况与带关键 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;while (unordered_map1.size() < 6){//若容器尚未含有带等价关键的元素,则插入元素到容器中。1-2) 插入 value 。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++){//1) 返回拥有比较等于指定参数 key 的关键的元素数,因为此容器不允许重复故为 1 或 0 。std::cout << "unordered_map1 count(" << cit->first << ") :    "<< unordered_map1.count(cit->first) << std::endl;}std::cout << std::endl;for (std::unordered_map::const_iterator cit =unordered_map1.cbegin(); cit != unordered_map1.end(); cit++){//1,2) 寻找键等于 key 的的元素。std::unordered_map::const_iterator fit = unordered_map1.find(cit->first);std::cout << "unordered_map1 find(" << cit->first << ") :    " << *fit << std::endl;}std::cout << std::endl;for (std::unordered_map::const_iterator cit =unordered_map1.cbegin(); cit != unordered_map1.end(); cit++){//1,2) 返回容器中所有键等于 key 的元素范围。//范围以二个迭代器定义,第一个指向所需范围的首元素,而第二个指向范围的尾后一位元素。std::pair::const_iterator,std::unordered_map::const_iterator> pit = unordered_map1.equal_range(cit->first);std::cout << "unordered_map1 equal_range(" << cit->first << ") :  ";for (std::unordered_map::const_iterator it = pit.first; it != pit.second; it++){std::cout << *it << " ";}std::cout << std::endl;}std::cout << std::endl;return 0;
}

输出

 

相关内容

热门资讯

快评丨放大区位优势 发展赛事经... 转自:河北新闻网2025京津冀第七届公开水域游泳挑战赛暨千人横渡滹沱河活动,1000多名来自京津冀等...
纺织工业碳排放强度大幅降低 记者日前从中国纺织工业联合会获悉,近两年纺织服装行业平均碳排放强度降幅超14%,行业绿色发展步伐加快...
马里多地遇袭 军方打死80多名... 新华社达喀尔7月1日电(记者司源)巴马科消息:马里军方1日发表声明说,马里中部和西部多地当天遭到恐怖...
民航局回应充电宝新规未设缓冲期 【#民航局回应充电宝新规未设缓冲期#】日前,中国民航局发布紧急通知,自6月28日起,禁止旅客携带没有...
【方正金工】6月行业组合战胜基... (转自:市场投研资讯)本文来自方正证券研究所于2025年7月1日发布的报告《6月行业组合战胜基准0....
兰州市城关区今年将试点“均衡入... 城关区将试点“均衡入学”“派位入学”每日甘肃网7月2日讯 据兰州晚报报道 2025年城关区小学招生方...
“最讨厌的功能终于取消了”,微... 7月1日下午,#微信可以不接收共同好友点赞提醒了#冲上热搜第一。近日,微信朋友圈灰度上线“不接收共同...
扬帆新材:暂未了解到产品可直接... 投资者提问:行业内众多企业都在积极探索巯基化合物在固态电池中的应用。扬帆新材在巯基化合物应用于固态电...
国内成品油价“三连涨” 机构:... 中国网财经7月2日讯 国家发展改革委7月1日发布通知,根据近期国际市场油价变化情况,按照现行成品油价...
国泰海通证券:长期看好固态电池... 每经AI快讯,国泰海通证券研报表示,固态电池具备高能量密度、高安全性,能够满足车端、低空、人形机器人...