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. Visaul Studio 密钥

    vs professional 2015 简体中文版  :HMGNV-WCYXV-X7G9W-YCX63-B98R2

  2. browsersync 插件

    自从发现了这个 browsersync 插件 ... 在也不用每次改一行代码就去手动刷新 HTML 页面了省去了很多繁琐的操作,当有多个显示器的时候,更加的方便,在IDEA上编辑代码之后,点击 com ...

  3. 【Linux 运维】linux系统关机、重启、注销命令

    linux 关机.重启.注销命令: 关机命令: shutdown -h now 立刻关机(生产常用) shutdown -h  +1  一分钟后关机      (    shutdown -c 可以将 ...

  4. ubuntu ssh配置

    Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over ...

  5. LeetCode 108——将有序数组转化为二叉搜索树

    1. 题目 2. 解答 一棵高度平衡的二叉搜索树意味着根节点的左右子树包含相同数量的节点,也就是根节点为有序数组的中值. 因此,我们将数组的中值作为根节点,然后再递归分别得到左半部分数据转化的左子树和 ...

  6. 前端整合MathjaxJS的配置笔记

    这篇文章是我给Pinghsu主题添加数学公式功能的一个小教程,包含我大量的官方文档阅读后的实践,跟着这篇配置教程走,你可以做到给任何一个需要数学公式的站点添加支持. 教程如标题所述是针对 Mathja ...

  7. Python3 Tkinter-Menu

    1.创建 from tkinter import * root=Tk() menubar=Menu(root) def hello(): print('Hello Menu!') for item i ...

  8. SpringBoot日志配置(详解) 涉及控制台输出日志、生成日志文件、日志级别修改、hibernate日志不输出

    写在前面 本篇主要讲述日志配置,看完本篇可以解决下述问题, 控制台输出日志.生成日志文件.日志级别修改.hibernate日志不输出 Git Demo Path:https://github.com/ ...

  9. Linux error:No space left on device

    一台Oracle数据库服务器在关机重启后,Oracle监听无法启动,提示错误 Linux error:no space left on device 提示可知:问题是出在磁盘空间不足 但是初步查看分区 ...

  10. 11.24Daily Scrum(4)

    人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.1007 实现视频浏览的功能 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.1008 实现视频浏 ...