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

输出

 

相关内容

热门资讯

常用商务英语口语   商务英语是以适应职场生活的语言要求为目的,内容涉及到商务活动的方方面面。下面是小编收集的常用商务...
六年级上册英语第一单元练习题   一、根据要求写单词。  1.dry(反义词)__________________  2.writ...
复活节英文怎么说 复活节英文怎么说?复活节的英语翻译是什么?复活节:Easter;"Easter,anniversar...
2008年北京奥运会主题曲 2008年北京奥运会(第29届夏季奥林匹克运动会),2008年8月8日到2008年8月24日在中华人...
英语道歉信 英语道歉信15篇  在日常生活中,道歉信的使用频率越来越高,通过道歉信,我们可以更好地解释事情发生的...
六年级英语专题训练(连词成句... 六年级英语专题训练(连词成句30题)  1. have,playhouse,many,I,toy,i...
上班迟到情况说明英语   每个人都或多或少的迟到过那么几次,因为各种原因,可能生病,可能因为交通堵车,可能是因为天气冷,有...
小学英语教学论文 小学英语教学论文范文  引导语:英语教育一直都是每个家长所器重的,那么有关小学英语教学论文要怎么写呢...
英语口语学习必看的方法技巧 英语口语学习必看的方法技巧如何才能说流利的英语? 说外语时,我们主要应做到四件事:理解、回答、提问、...
四级英语作文选:Birth ... 四级英语作文范文选:Birth controlSince the Chinese Governmen...
金融专业英语面试自我介绍 金融专业英语面试自我介绍3篇  金融专业的学生面试时,面试官要求用英语做自我介绍该怎么说。下面是小编...
我的李老师走了四年级英语日记... 我的李老师走了四年级英语日记带翻译  我上了五个学期的小学却换了六任老师,李老师是带我们班最长的语文...
小学三年级英语日记带翻译捡玉... 小学三年级英语日记带翻译捡玉米  今天,我和妈妈去外婆家,外婆家有刚剥的`玉米棒上带有玉米籽,好大的...
七年级英语优秀教学设计 七年级英语优秀教学设计  作为一位兢兢业业的人民教师,常常要写一份优秀的教学设计,教学设计是把教学原...
我的英语老师作文 我的英语老师作文(通用21篇)  在日常生活或是工作学习中,大家都有写作文的经历,对作文很是熟悉吧,...
英语老师教学经验总结 英语老师教学经验总结(通用19篇)  总结是指社会团体、企业单位和个人对某一阶段的学习、工作或其完成...
初一英语暑假作业答案 初一英语暑假作业答案  英语练习一(基础训练)第一题1.D2.H3.E4.F5.I6.A7.J8.C...
大学生的英语演讲稿 大学生的英语演讲稿范文(精选10篇)  使用正确的写作思路书写演讲稿会更加事半功倍。在现实社会中,越...
VOA美国之音英语学习网址 VOA美国之音英语学习推荐网址 美国之音网站已经成为语言学习最重要的资源站点,在互联网上还有若干网站...
商务英语期末试卷 Part I Term Translation (20%)Section A: Translate ...