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的更多相关文章

  1. WebShell代码分析溯源(十一)

    WebShell代码分析溯源(十一) 一.一句话变形马样本 <?php $e = $_REQUEST['e'];declare(ticks=1);register_tick_function ( ...

  2. stout代码分析之零

    最近在使用mesos做高可用设计,在编译的过程中注意到mesos依赖stout,一个仅仅含有头文件的c++基础库.stout代码简洁,设计优雅,值得一读. stout从内容上可细分为以下几块: Pri ...

  3. stout代码分析之十:c++11之move和forward

    stout中大量使用了c++11的特性,而c++11中move和forward大概是最神奇的特性了. 左值和右值的区别 ; // a是左值,0是右值 int b = rand(); // b是左值,r ...

  4. stout代码分析之九:c++11容器新特性

    stout大量使用了c++11的一些新特性,使用这些特性有利于简化我们的代码,增加代码可读性.以下将对一些容器的新特性做一个总结.主要两方面: 容器的初始化,c++11中再也不用手动insert或者p ...

  5. stout代码分析之八:cache类

    stout中实现了LRU cache.cache的成员如下: public: typedef std::list<Key> list; typedef std::tr1::unordere ...

  6. stout代码分析之七:Result类

    Result类似于Option和Try类的组合,内部有三种状态 enum State { SOME, NONE, ERROR }; SOME表示Result对象有值 NONE表示Result对象值为空 ...

  7. stout代码分析之六:Stopwatch

    在进行性能测试时,经常需要计算某个函数执行的时长.stout中的Stopwatch类可实现纳秒精度的计时. Stopwatch内部使用timespec记录开始和技术时间.   timeval和time ...

  8. stout代码分析之五:UUID类

    UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识. stout中UUID类继承自boost::uuids::uuid.api如下: random, 产生一个UUID对象 ...

  9. stout代码分析之一:Duration类

    Duration类用于表示时间长度,可精确到纳秒. 代码实现在duration.hpp中,测试代码:duration_tests.cpp 相关api如下: parse, 将字符串转化成Duration ...

随机推荐

  1. 浙江天搜科技落棋人工智能,加速AI产业布局

    8月31日,2018年IFA大展在德国柏林正式开幕.IFA是全球三大消费电子展之一,在世界范围内久负盛名,被誉为“未来科技风向标”.在这个万众瞩目的展会上,号称“给智能世界铺上云的跑道,装上智能发动机 ...

  2. Wordcount -- MapReduce example -- Mapper

    Mapper maps input key/value pairs into intermediate key/value pairs. E.g. Input: (docID, doc) Output ...

  3. FPGA学习-VGA接口

    一般FPGA开发板的VGA会向用户暴露两共五个种接口,第一种是时序信号,用于同步传输和显示:第二种是色彩信号,用于随着时序把色彩显示到显示器上 时序接口 行同步信号-用于指示一行内像素的显示 场同步信 ...

  4. 水管工游戏:dfs(递归)

    添柴网这题好想不能评测,所以不确保代码的正确性 题目描述: 这小节有点难,看不太懂可以跳过哦.最近小哼又迷上一个叫做水管工的游戏.游戏的大致规则是这样的.一块矩形土地被分为N * M的单位正方形,现在 ...

  5. Map Reduce Application(Partitioninig/Binning)

    Map Reduce Application(Partitioninig/Group data by a defined key) Assuming we want to group data by ...

  6. hosts_allow配置了却不生效

    hosts_allow配置了却不生效 配置了两台白名单的机器,一台生效一台不生效,google后的结果都是更新libwrap.so  安装openssh等等..(问题还是没有解决) 经过对比发现,原来 ...

  7. 团队作业7——第二次项目冲刺-Beta版本项目计划

    上一个阶段的总结: 在Alpha阶段,我们小组已近完成了大部分的功能要求,小组的每一个成员都发挥了自己的用处.经过了这么久的磨合,小组的成员之间越来越默契,相信在接下来的合作中,我们的开发速度会越来越 ...

  8. LintCode-380.两个链表的交叉

    两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 注意事项 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 样例 下列两 ...

  9. string字符串比较和替换

    我用的是小写的string!! #include <string> #include <iostream> using namespace std; int main() { ...

  10. Kafka性能之道

    Kafka高性能之道 高效使用磁盘 零拷贝 批处理和压缩 Partition ISR 高效使用磁盘 >顺序写cipan >Append Only(数据不更新,无记录级的数据删除,只会整个s ...