stout代码分析之十一:hashmap和multihashmap
hashmap是std::unordered_map的子类,前者对后者的接口做了进一步封装。
- hashmap的移动构造函数:
hashmap(std::map<Key, Value>&& map)
{
// NOTE: We're using 'insert' here with a move iterator in order
// to avoid copies because we know we have an r-value paramater.
std::unordered_map<Key, Value, Hash, Equal>::insert(
std::make_move_iterator(map.begin()),
std::make_move_iterator(map.end()));
}
std::make_move_iterator会将map.begin和map.end()转化为std::iterator类型,从而能够使用unordered_map::insert的右值语义。
- hashmap从initializer_list构造
hashmap(std::initializer_list<std::pair<Key, Value>> list)
{
std::unordered_map<Key, Value, Hash, Equal>::reserve(list.size()); for (auto iterator = list.begin(); iterator != list.end(); ++iterator) {
std::unordered_map<Key, Value, Hash, Equal>::emplace(
iterator->first,
iterator->second);
}
}
这样就可以直接初始化hash_map,如:
hashmap<int, std::string> a = {{, "one"}, {, "two"}, {, "three"}};
- 其他api
- put, get
- contains, containsValue
- keys, values
示例代码如下:
#include "stout/hashmap.hpp"
#include <string>
#include <iostream> int main()
{
hashmap<int, std::string> a = {{, "one"}, {, "two"}, {, "three"}}; if (a.contains())
std::cout << "a contains 1" << std::endl; if (a.containsValue("one"))
std::cout << "a containsValue one" << std::endl; auto b = a.get();
if (b.isSome())
std::cout << "from a get " << b.get() << std::endl; auto c = a.keys();
for (const int& x : c)
std::cout << x << std::endl; auto d = a.values();
for (const std::string& x : d)
std::cout << x << std::endl;
return ;
}
multihashmap是std::unordered_multimap的派生类,同样的,前者对后者的接口也进行了一些封装。
- 移动构造函数于hashmap相似
- initializer_list构造函数与hashmap相似
- api
- put, get, 注意get返回的是std::list类型
- keys
- remove,既可去除某个key对应的所有键值对,又可以去除指定的键值对.
- contains,既可判断某个key是否在该容器中,又可判断某对key-value是否在该容器中
stout代码分析之十一:hashmap和multihashmap的更多相关文章
- WebShell代码分析溯源(十一)
WebShell代码分析溯源(十一) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];declare(ticks=1);register_tick_function ( ...
- stout代码分析之零
最近在使用mesos做高可用设计,在编译的过程中注意到mesos依赖stout,一个仅仅含有头文件的c++基础库.stout代码简洁,设计优雅,值得一读. stout从内容上可细分为以下几块: Pri ...
- stout代码分析之十:c++11之move和forward
stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...
- stout代码分析之九:c++11容器新特性
stout大量使用了c++11的一些新特性,使用这些特性有利于简化我们的代码,增加代码可读性.以下将对一些容器的新特性做一个总结.主要两方面: 容器的初始化,c++11中再也不用手动insert或者p ...
- stout代码分析之八:cache类
stout中实现了LRU cache.cache的成员如下: public: typedef std::list<Key> list; typedef std::tr1::unordere ...
- stout代码分析之七:Result类
Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...
- stout代码分析之六:Stopwatch
在进行性能测试时,经常需要计算某个函数执行的时长.stout中的Stopwatch类可实现纳秒精度的计时. Stopwatch内部使用timespec记录开始和技术时间. timeval和time ...
- stout代码分析之五:UUID类
UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识. stout中UUID类继承自boost::uuids::uuid.api如下: random, 产生一个UUID对象 ...
- stout代码分析之一:Duration类
Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...
随机推荐
- leetcode-对称二叉树
对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2, ...
- SpringBoot在IDEA下使用JPA
1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...
- 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)
[SSH进阶之路]Struts + Spring + Hibernate 进阶开端(一) 标签: hibernatespringstrutsssh开源框架 2014-08-29 07:56 9229人 ...
- Servlet过滤器介绍之原理分析
zhangjunhd 的BLOG 写留言去学院学习发消息 加友情链接进家园 加好友 博客统计信息 51CTO博客之星 用户名:zhangjunhd 文章数:110 评论数:858 访问量:19 ...
- 二 Capacity Scheduler 计算能力调度器
官网的写的太难懂,参考:http://www.360doc.com/content/14/0603/14/14935022_383254798.shtml Capacity Scheduler 一种可 ...
- js如何使浏览器允许脚本异步加载
js如何使浏览器允许脚本异步加载 如果脚本体积很大,下载和执行的时间就会很长,因此造成浏览器堵塞,用户会感觉到浏览器“卡死”了,没有任何响应.这显然是很不好的体验,所以浏览器允许脚本异步加载,下面就是 ...
- Python函数的内省-Introspection
Python函数可以进行内省-Introspection,查看函数内部的细节,方式就是使用函数的__code__属性. def func(a, b = 2): return a + b >> ...
- 推荐形参使用常量引用:void func(const T &);
一.声明为const的原因: 把函数不会改变的形参定义成普通的引用会带给函数的调用者一种误导,即函数可以修改它的实参的值: 限制函数所能接受的实参类型,如不能把const对象.字面值或者需要类型转换的 ...
- Python-期末练习
1.骑车与走路:我们的校园很大很大很大大大大大……,骑个自行车去办事会很快,比如取个快递了,到其他宿舍楼找个同(nv)学(you)了.但实际上,并非去办任何事情都是骑车快,因为骑车总要找车.开锁.停车 ...
- 软工Hello World!团队第二周博客汇总
2017.10.20-2017.10.26 Scrum会议: 第一天:http://www.cnblogs.com/liusx0303/p/7704482.html 第二天:http://www.cn ...