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;
}

输出

 

相关内容

热门资讯

股价走高触发强赎 7月将有两只... 近日,银行正股股价表现强势,多只银行转债触发强制赎回条款。7月1日是杭银转债最后一个交易日,其最后转...
300548,“改名”,股价历... 科技股和顺周期板块再现“跷跷板”走势。今天上午,顺周期板块走强,银行、有色金属、白酒、新能源等板块上...
明阳电路在昆山投资成立集成电路... 人民财讯7月2日电,企查查APP显示,近日,昆山华芯微测集成电路有限公司成立,法定代表人为窦旭才,经...
民生银行“易创E贷”获“202... 在近日举行的第七届数字普惠金融大会上,民生银行“易创E贷”产品凭借在服务科技型中小微企业的创新与实践...
机器人大军逼近,很快,亚马逊的...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! 亚马逊正迅速接近仓库...
广东省教育考试院通报:不存在中... 7月2日,广东省教育考试院发布通报称,7月1日下午广东省初中学业水平考试(以下简称“中考”)数学科目...
国际油价承压 中东油企拟放缓全... 财联社7月2日讯(编辑 秦嘉禾)在国际油价下行压力加剧背景下,中东两大国有能源巨头——沙特阿美(Sa...
光伏50ETF、光伏龙头ETF... 光伏设备板块走强,亿晶光电、欧晶科技涨停。银华光伏50ETF、汇添富光伏龙头ETF、浦银安盛光伏龙头...
世界银行发布重磅预测:黄金今年... 错过上半年,别再错过下半年!世界银行继续看好贵金属前景,黄金、白银、铂金或将继续延续强势……世界银行...
洪灝称高分红投资策略应继续有所... 【#洪灝称高分红投资策略应继续有所表现#】中国股市近期一个显著趋势是分红和回购活动显著增加。其中,许...