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

输出

 

相关内容

热门资讯

结婚典礼感谢致辞 结婚典礼感谢致辞(通用15篇)  在平平淡淡的学习、工作、生活中,要用到致辞的情况还是蛮多的,致辞要...
大鱼海棠配音台词 大鱼海棠配音台词  蛰伏十二年的动画《大鱼海棠》,终于上映了。虽然剧情被很多人吐槽,但美得不像话的画...
小学毕业典礼主持词 小学毕业典礼主持词实用范文  活动对象的不同,主持词的写作风格也会大不一样。在当今中国社会,司仪等是...
篮球赛闭幕词 篮球赛闭幕词(精选15篇)篮球赛闭幕词1各位来宾、同志们:  为期xx天的xxx第二届职工篮球赛,在...
元旦联欢晚会主持词 元旦联欢晚会主持词范文(精选9篇)  主持词分为会议主持词、晚会主持词、活动主持词、婚庆主持词等。在...
大型文艺演出主持词 大型文艺演出主持词  主持词可以采用和历史文化有关的表述方法去写作以提升活动的文化内涵。在当今中国社...
开学典礼致辞 开学典礼致辞15篇  在日常生活或是工作学习中,大家都经常接触到致辞吧,致辞具有“礼仪性”或“仪式化...
农村婚礼司仪主持词 农村婚礼司仪主持词15篇  主持词需要富有情感,充满热情,才能有效地吸引到观众。在当今不断发展的世界...
如何主持会议参考 如何主持会议范文参考  1.宣布会议开始。会议时间到后,若全体与会人员都已到位(至少重要的与会人员已...
韩剧漂亮男人经典台词 韩剧漂亮男人经典台词  1、答案都是在当事人手里的,不知道人们为什么,明明是自己非常了解的问题,但是...
外公七十大寿祝寿词 外公七十大寿祝寿词  外公七十大寿祝寿词  尊敬的外公、外婆、各位长辈、各位来宾:    大家好!今...
《香瓜七兄弟》经典台词 《香瓜七兄弟》经典台词  1. 阿丽呀,我哪好,我改还不行吗  2. 记不记得上回你尿炕 阿姨来了你...
孙子满月酒爷爷致辞 孙子满月酒爷爷致辞(通用6篇)  无论是身处学校还是步入社会,大家都不可避免地会接触到致辞吧,致辞具...
国庆文艺汇演主持词 国庆文艺汇演主持词  主持词要注意活动对象,针对活动对象写相应的主持词。在当今社会中,主持人在活动中...
中秋晚会主持词开场 中秋晚会主持词开场  主持词是主持人在台上表演的灵魂之所在。在现在的社会生活中,主持人的需求越来越高...
婚礼现场新郎致辞 婚礼现场新郎致辞各位亲朋好友:  大家好!  人生能有几次最难忘、最幸福的时刻,今天我才真正从内心里...
班会主持稿 班会主持稿(15篇)  在生活中,我们越来越需要主持稿,主持稿是主持人于节目进行过程中串联节目的稿件...
幼儿园开学典礼的主持词 幼儿园开学典礼的主持词范文(精选6篇)  根据活动对象的不同,需要设置不同的主持词。在当下这个社会中...
教师节晚会活动主持词 教师节晚会活动主持词(精选10篇)  主持词是主持人在台上表演的灵魂之所在。在当下的中国社会,很多场...
同学聚会的主持词 同学聚会的主持词3篇  中学时代,是一个人一生中最美好的时光。小编为大家整理了同学聚会的主持词3篇,...