c++11 标准模板(STL)(std::unordered_map)(九)
创始人
2024-06-01 10:24:49
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 起)

查找

 访问指定的元素,同时进行越界检查

std::unordered_map::at

T& at( const Key& key );

(1)(C++11 起)

const T& at( const Key& key ) const;

(2)(C++11 起)

 返回到拥有等于 key 的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。

参数

key-要找到的元素的关键

返回值

到请求元素的被映射值的引用

异常

若容器无拥有指定 key 的元素则为 std::out_of_range

复杂度

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

访问或插入指定的元素

std::unordered_map::operator[]

T& operator[]( const Key& key );

(1)(C++11 起)

T& operator[]( Key&& key );

(2)(C++11 起)

 返回到映射到等于 key 的关键的值的引用,若这种关键不存在则进行插入。

1) 若关键不存在,则插入从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 原位构造的 value_type 对象。此函数等价于 return this->try_emplace(key).first->second; 。 (C++17 起)
使用默认分配器时,这导致从 key 复制构造关键,并值初始化被映射值。

- value_type 必须从 std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() 可就位构造 (EmplaceConstructible) 。使用默认分配器时,这表明 key_type 必须可复制构造 (CopyConstructible) 而 mapped_type 必须可默认构造 (DefaultConstructible) 。

2) 若关键不存在,则插入从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 原位构造的 value_type 对象。此函数等价于 return this->try_emplace(std::move(key)).first->second; 。 (C++17 起)
使用默认分配器时,这导致从 key 移动构造关键,并值初始化被映射值。

- value_type 必须从 std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() 可就位构造 (EmplaceConstructible) 。使用默认分配器时,这表明 key_type 必须可移动构造 (MoveConstructible) mapped_type 必须可默认构造 (DefaultConstructible) 。

若插入发生且导致容器的重哈希,则所有迭代器被非法化。否则迭代器不受影响。重哈希仅若新元素数量大于 max_load_factor()*bucket_count() 才发生。

参数

key-要寻找的元素关键

返回值

若不存在拥有关键 key 的元素,则为到新元素被映射值的引用。否则为到既存的关键等价于 key 的元素的被映射值的引用。

异常

若任何操作抛出异常,则插入无效果。

复杂度

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

注意

出版的 C++11 和 C++14 标准中,指定此函数要求 mapped_type可默认插入 (DefaultInsertable) 且 key_type可复制插入 (CopyInsertable) 或可移动插入 (MoveInsertable) 到 *this 。此规定有缺陷并为 LWG 问题 2469 所修复,而上面的描述合并了该问题的解决方案。

然而,已知一个实现( libc++ )通过二个分离的分配器 construct() 调用构造 key_typemapped_type 对象,可认为如发布时的标准所要求,而非原位构造 value_type 对象。

operator[] 非 const ,因为若不关键不存在则它插入关键。若此行为非所欲或容器为 const ,则可用 at()

insert_or_assign() 返回的信息多于 operator[] ,而且不要求 mapped_type 可默认构造。

(C++17 起)

调用示例 

#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 const at:   " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << "     ";//返回到拥有等于 key 的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。std::cout << "value: " << unordered_map1.at(cit->first);std::cout << std::endl;}std::cout << std::endl;std::cout << "unordered_map1 at:   " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << "     ";//返回到拥有等于 key 的关键的元素被映射值的引用。若无这种元素,则抛出 std::out_of_range 类型异常。string value = unordered_map1.at(cit->first);unordered_map1.at(cit->first) = value + value;std::cout << "value: " << unordered_map1.at(cit->first);std::cout << std::endl;}std::cout << std::endl;std::cout << std::endl;std::unordered_map unordered_map2;std::cout << "unordered_map2 const Key& key :   " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << "     ";//返回到映射到等于 key 的关键的值的引用,若这种关键不存在则进行插入。std::cout << "value: " <<  unordered_map2[cit->first] << "   ";unordered_map2[cit->first] = cit->second;std::cout << "value: " << unordered_map2[cit->first];std::cout << std::endl;}std::cout << std::endl;std::unordered_map unordered_map3;//移动语义std::cout << "unordered_map3  Key&& key  :   " << std::endl;for (std::unordered_map::const_iterator cit = unordered_map1.cbegin();cit != unordered_map1.end(); cit++){std::cout << "key: " << cit->first << "     ";//返回到映射到等于 key 的关键的值的引用,若这种关键不存在则进行插入。std::cout << "value: " << unordered_map3[cit->first] << "   ";unordered_map3[cit->first] = cit->second;std::cout << "value: " << unordered_map3[std::move(cit->first)];std::cout << std::endl;}std::cout << std::endl;return 0;
}

输出

 

相关内容

热门资讯

李白《梦游天姥吟留别》赏析 李白《梦游天姥吟留别》赏析(5篇)李白《梦游天姥吟留别》赏析1  梦游天姥(mǔ)吟留别  (也作《...
手机怎么用百度翻译文言文 手机怎么用百度翻译文言文  第一步:  下载“手机百度翻译”软件,打开百度翻译软件,在源语言一栏中点...
《都市歌手》第十七集 《都市歌手》第十七集都市歌手第十七集我开始喜欢你(上)人物:金紫燕、穆双毅、隋世兵、霍智广、屠天信、...
《假如生活欺骗了你》2 《假如生活欺骗了你》2教学设计:诗歌导入人生道路,不可能一帆风顺,有时真的不遂人愿。假如你觉得生活欺...
周易是什么东西 周易是什么东西  《周易》这部特殊的经典,在我国历史上地位非常高。儒家认为它是第一经典,所谓的五经之...
《般若波罗蜜多心经》浅释:四... 《般若波罗蜜多心经》浅释:四圣谛  我们都知道,佛法分为「世间」和「出世间」两种,而出世间又有大乘、...
虞卿谓春申君原文及翻译 虞卿谓春申君原文及翻译  在日常学习、工作或生活中,大家一定避免不了阅读课文,阅读有利于提升我们的文...
苏轼:题西林壁 苏轼:题西林壁  题西林壁  横看成岭侧成峰,  远近高低各不同。  不识庐山真面目,  只缘身在此...
《论语·季氏》文言文阅读题 《论语·季氏》文言文阅读题  陈亢问于伯鱼①曰:“子亦有异闻乎?”对曰:“未也。尝独立,鲤趋而过庭。...
孔子见罗雀者文言文翻译 孔子见罗雀者文言文翻译  孔子见罗雀者是选自《孔子家语》的,那么,下面是小编给大家整理收集的孔子见罗...
乐羊子妻翻译和原文 乐羊子妻翻译和原文  《乐羊子妻》(yuèyángzǐqī),是一篇人物传记。它通过两个小故事,赞扬...
爱情是什么的排比句 爱情是什么的排比句  1、爱情是一杯茶,香醇可口,伴着甜味,夹着苦味;爱情是一把钥匙,打开心灵,享受...
游白水书付过原文及赏析 游白水书付过原文及赏析  原文:  游白水书付过  [宋代]苏轼  绍圣元年十月十二日,与幼子过游白...
平凡的世界电视剧 平凡的世界电视剧  平凡的世界剧情介绍  上世纪70年代,自尊好强的农家子弟孙少平在原西县高中读书,...
苏轼《西江月》原文翻译及赏析 苏轼《西江月》原文翻译及赏析  西江月·顷在黄州 苏轼  顷在黄州,春夜行蕲水中,过酒家饮,酒醉,乘...
管鲍之交文言文翻译 管鲍之交文言文翻译  文言文是中国古代的一种书面语言,主要包括以先秦时期的口语为基础而形成的书面语。...
核舟记原文及翻译朗诵 核舟记原文及翻译朗诵  《核舟记》是由明代作家魏学洢撰写的一篇文章,由同时代学者张潮编选到《虞初新志...
苏轼《浣溪沙·细雨斜风作晓寒... 苏轼《浣溪沙·细雨斜风作晓寒》的阅读答案附翻译赏析  细雨斜风作晓寒,淡烟疏柳媚晴滩。入准清洛渐漫漫...
霁雪原文翻译及赏析 霁雪原文翻译及赏析霁雪原文翻译及赏析1  原文:  霁雪/韩舍人书窗残雪  风卷寒云暮雪晴,江烟洗尽...
新版论语原文和翻译 新版论语十则原文和翻译  子曰:由,诲女知之乎?知之为知之,不知为不知,是知也。(《为政》)  孔子...