C++中的hash_map和map的区别

STL之hash_map hash_map简介 hash_map的用法和map是一样的,提供了insert,size,count等操作,并且里面的元素也是以pair类型来存贮的。虽然对外部提供的函数和数据类型是一致的,但是其底层实现是完全不同的,map底层的数据结构是rb_tree而,hansh_map却是哈希表来实现的。 总体来说,hash_map 查找速度会比map快,而且查找速度基本和数据量大小无关,属于常数级别 阅读详情

hash_map和map的区别在哪里?
构造函数。hash_map需要hash函数,等于函数;map只需要比较函数(小于函数). 
存储结构。hash_map采用hash表存储,map一般采用红黑树(RB Tree)实现。因此其memory数据结构是不一样的。 
什么时候需要用hash_map,什么时候需要用map?
总体来说,hash_map 查找速度会比map快,而且查找速度基本和数据数据量大小,属于常数级别;而map的查找速度是log(n)级别。并不一定常数就比log(n)小,hash还有hash函数的耗时,明白了吧,如果你考虑效率,特别是在元素达到一定数量级时,考虑考虑hash_map。但若你对内存使用特别严格,希望程序尽可能少消耗内存,那么一定要小心,hash_map可能会让你陷入尴尬,特别是当你的hash_map对象特别多时,你就更无法控制了,而且hash_map的构造速度较慢。

现在知道如何选择了吗?权衡三个因素: 查找速度, 数据量, 内存使用。

详细介绍见C++中的hash_map和map的区别_鱼思故渊的专栏-优快云博客_c++ hash_map

1)为什么需要hash_map
用过map吧?map提供一个很常用的功能,那就是提供key-value的存储和查找功能。例如,我要记录一个人名和相应的存储,而且随时增加,要快速查找和修改:

岳不群-华山派掌门人,人称君子剑
张三丰-武当掌门人,太极拳创始人
东方不败-第一高手,葵花宝典
...
    这些信息如果保存下来并不复杂,但是找起来比较麻烦。例如我要找"张三丰"的信息,最傻的方法就是取得所有的记录,然后按照名字一个一个比较。如果要速度快,就需要把这些记录按照字母顺序排列,然后按照二分法查找。但是增加记录的时候同时需要保持记录有序,因此需要插入排序。考虑到效率,这就需要用到二叉树。讲下去会没完没了,如果你使用STL 的map容器,你可以非常方便的实现这个功能,而不用关心其细节。关于map的数据结构细节,感兴趣的朋友可以参看学习STL map, STL set之数据结构基础。看看map的实现:

#include <map>
 
#include <string>
 
using namespace std;
 
...
 
map<string, string> namemap;
//增加。。。
namemap["岳不群"]="华山派掌门人,人称君子剑";
namemap["张三丰"]="武当掌门人,太极拳创始人";
namemap["东方不败"]="第一高手,葵花宝典";
...

//查找。。
if(namemap.find("岳不群") != namemap.end()){
        ...
}

 不觉得用起来很easy吗?而且效率很高,100万条记录,最多也只要20次的string.compare的比较,就能找到你要找的记录;200万条记录事,也只要用21次的比较。

    速度永远都满足不了现实的需求。如果有100万条记录,我需要频繁进行搜索时,20次比较也会成为瓶颈,要是能降到一次或者两次比较是否有可能?而且当记录数到200万的时候也是一次或者两次的比较,是否有可能?而且还需要和map一样的方便使用。

    答案是肯定的。这时你需要has_map. 虽然hash_map目前并没有纳入C++ 标准模板库中,但几乎每个版本的STL都提供了相应的实现。而且应用十分广泛。在正式使用hash_map之前,先看看hash_map的原理。

2)数据结构:hash_map原理
    这是一节让你深入理解hash_map的介绍,如果你只是想囫囵吞枣,不想理解其原理,你倒是可以略过这一节,但我还是建议你看看,多了解一些没有坏处。

hash_map基于hash table(哈希表)。 哈希表最大的优点,就是把数据的存储和查找消耗的时间大大降低,几乎可以看成是常数时间;而代价仅仅是消耗比较多的内存。然而在当前可利用内存越来越多的情况下,用空间换时间的做法是值得的。另外,编码比较容易也是它的特点之一。

    其基本原理是:使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数,也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标,hash值)相对应,于是用这个数组单元来存储这个元素;也可以简单的理解为,按照关键字为每一个元素“分类”,然后将这个元素存储在相应“类”所对应的地方,称为桶。

    但是,不能够保证每个元素的关键字与函数值是一一对应的,因此极有可能出现对于不同的元素,却计算出了相同的函数值,这样就产生了“冲突”,换句话说,就是把不同的元素分在了相同的“类”之中。 总的来说,“直接定址”与“解决冲突”是哈希表的两大特点。

    hash_map,首先分配一大片内存,形成许多桶。是利用hash函数,对key进行映射到不同区域(桶)进行保存。其插入过程是:

  • 得到key 
  • 通过hash函数得到hash值 
  • 得到桶号(一般都为hash值对桶数求模) 
  • 存放key和value在桶内。 

其取值过程是:

  • 得到key
  • 通过hash函数得到hash值 
  • 得到桶号(一般都为hash值对桶数求模) 
  • 比较桶的内部元素是否与key相等,若都不相等,则没有找到。 
  • 取出相等的记录的value。 

    hash_map中直接地址用hash函数生成,解决冲突,用比较函数解决。这里可以看出,如果每个桶内部只有一个元素,那么查找的时候只有一次比较。当许多桶内没有值时,许多查询就会更快了(指查不到的时候).

    由此可见,要实现哈希表, 和用户相关的是:hash函数和比较函数。这两个参数刚好是我们在使用hash_map时需要指定的参数。

3) hash_map 使用
    一个简单实例
不要着急如何把"岳不群"用hash_map表示,我们先看一个简单的例子:随机给你一个ID号和ID号相应的信息,ID号的范围是1~2的31次方。如何快速保存查找。

#include <hash_map>
 
#include <string>
 
using namespace std;
 
int main(){
 
hash_map<int, string> mymap;
 
mymap[9527]="唐伯虎点秋香";
 
mymap[1000000]="百万富翁的生活";
 
mymap[10000]="白领的工资底线";
 
...
 
if(mymap.find(10000) != mymap.end()){
 
...
 
}

      够简单,和map使用方法一样。这时你或许会问?hash函数和比较函数呢?不是要指定么?你说对了,但是在你没有指定hash函数和比较函数的时候,你会有一个缺省的函数,看看hash_map的声明,你会更加明白。下面是SGI STL的声明:

 
template <class _Key, class _Tp, class _HashFcn = hash<_Key>,
 
class _EqualKey = equal_to<_Key>,
 
class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
 
class hash_map
 
{
 
...
 
}

也就是说,在上例中,有以下等同关系:

...
 
hash_map<int, string> mymap;
 
//等同于:
 
hash_map<int, string, hash<int>, equal_to<int> > mymap;

Alloc我们就不要取关注太多了(希望深入了解Allocator的朋友可以参看标准库 STL :Allocator能做什么)

hash_map 的hash函数

hash< int>到底是什么样子?看看源码:

struct hash<int> {
 
size_t operator()(int __x) const { return __x; }
 
};
 
原来是个函数对象。在SGI STL中,提供了以下hash函数:
  
 
struct hash<char*>
 
struct hash<const char*>
 
struct hash<char>
 
struct hash<unsigned char>
 
struct hash<signed char>
 
struct hash<short>
 
struct hash<unsigned short>
 
struct hash<int>
 
struct hash<unsigned int>
 
struct hash<long>
 
struct hash<unsigned long>

也就是说,如果你的key使用的是以上类型中的一种,你都可以使用缺省的hash函数。当然你自己也可以定义自己的hash函数。对于自定义变量,你只能如此,例如对于string,就必须自定义hash函数。例如:

 
struct str_hash{
 
size_t operator()(const string& str) const
 
{
 
unsigned long __h = 0;
 
for (size_t i = 0 ; i < str.size() ; i ++)
 
__h = 5*__h + str[i];
 
return size_t(__h);
 
}
 
};

//如果你希望利用系统定义的字符串hash函数,你可以这样写:

 
struct str_hash{
 
size_t operator()(const string& str) const
 
{
 
return __stl_hash_string(str.c_str());
 
}
 
};
在声明自己的哈希函数时要注意以下几点:

 

使用struct,然后重载operator(). 
返回是size_t 
参数是你要hash的key的类型。 
函数是const类型的。 
如果这些比较难记,最简单的方法就是照猫画虎,找一个函数改改就是了。

现在可以对开头的"岳不群"进行哈希化了  . 直接替换成下面的声明即可:

map<string, string> namemap; 
//改为:
hash_map<string, string, str_hash> namemap;
其他用法都不用边。当然不要忘了吧str_hash的声明以及头文件改为hash_map。

你或许会问:比较函数呢?别着急,这里就开始介绍hash_map中的比较函数。

   hash_map 的比较函数
在map中的比较函数,需要提供less函数。如果没有提供,缺省的也是less< Key> 。在hash_map中,要比较桶内的数据和key是否相等,因此需要的是是否等于的函数:equal_to< Key> 。先看看equal_to的源码:

 

//本代码可以从SGI STL
 
//先看看binary_function 函数声明,其实只是定义一些类型而已。
 
template <class _Arg1, class _Arg2, class _Result>
 
struct binary_function {
 
typedef _Arg1 first_argument_type;
 
typedef _Arg2 second_argument_type;
 
typedef _Result result_type;
 
};
 
//看看equal_to的定义:
 
template <class _Tp>
 
struct equal_to : public binary_function<_Tp,_Tp,bool>
 
{
 
bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
 
};

如果你使用一个自定义的数据类型,如struct mystruct, 或者const char* 的字符串,如何使用比较函数?使用比较函数,有两种方法. 第一种是:重载==操作符,利用equal_to;看看下面的例子:

struct mystruct{
 
int iID;
 
int len;
 
bool operator==(const mystruct & my) const{
 
return (iID==my.iID) && (len==my.len) ;
 
}
 
};
    

这样,就可以使用equal_to< mystruct>作为比较函数了。另一种方法就是使用函数对象。自定义一个比较函数体:

 
struct compare_str{
 
bool operator()(const char* p1, const char*p2) const{
 
return strcmp(p1,p2)==0;
 
}
 
};

有了compare_str,就可以使用hash_map了。

typedef hash_map<const char*, string, hash<const char*>, compare_str> StrIntMap;
StrIntMap namemap;
namemap["岳不群"]="华山派掌门人,人称君子剑";
namemap["张三丰"]="武当掌门人,太极拳创始人";
namemap["东方不败"]="第一高手,葵花宝典";

hash_map 函数
hash_map的函数和map的函数差不多。具体函数的参数和解释,请参看:STL 编程手册:Hash_map,这里主要介绍几个常用函数。

hash_map(size_type n) 如果讲究效率,这个参数是必须要设置的。n 主要用来设置hash_map 容器中hash桶的个数。桶个数越多,hash函数发生冲突的概率就越小,重新申请内存的概率就越小。n越大,效率越高,但是内存消耗也越大。 
const_iterator find(const key_type& k) const. 用查找,输入为键值,返回为迭代器。 
data_type& operator[](const key_type& k) . 这是我最常用的一个函数。因为其特别方便,可像使用数组一样使用。不过需要注意的是,当你使用[key ]操作符时,如果容器中没有key元素,这就相当于自动增加了一个key元素。因此当你只是想知道容器中是否有key元素时,你可以使用find。如果你希望插入该元素时,你可以直接使用[]操作符。 
insert 函数。在容器中不包含key值时,insert函数和[]操作符的功能差不多。但是当容器中元素越来越多,每个桶中的元素会增加,为了保证效率,hash_map会自动申请更大的内存,以生成更多的桶。因此在insert以后,以前的iterator有可能是不可用的。 
erase 函数。在insert的过程中,当每个桶的元素太多时,hash_map可能会自动扩充容器的内存。但在sgi stl中是erase并不自动回收内存。因此你调用erase后,其他元素的iterator还是可用的。 
3)相关hash容器
hash 容器除了hash_map之外,还有hash_set, hash_multimap, has_multiset, 这些容器使用起来和set, multimap, multiset的区别与hash_map和map的区别一样,我想不需要我一一细说了吧。

4)其他
这里列几个常见问题,应该对你理解和使用hash_map比较有帮助。

    hash_map和map的区别在哪里?
构造函数。hash_map需要hash函数,等于函数;map只需要比较函数(小于函数). 
存储结构。hash_map采用hash表存储,map一般采用红黑树(RB Tree)实现。因此其memory数据结构是不一样的。 
    什么时候需要用hash_map,什么时候需要用map?
总体来说,hash_map 查找速度会比map快,而且查找速度基本和数据数据量大小,属于常数级别;而map的查找速度是log(n)级别。并不一定常数就比log(n)小,hash还有hash函数的耗时,明白了吧,如果你考虑效率,特别是在元素达到一定数量级时,考虑考虑hash_map。但若你对内存使用特别严格,希望程序尽可能少消耗内存,那么一定要小心,hash_map可能会让你陷入尴尬,特别是当你的hash_map对象特别多时,你就更无法控制了,而且hash_map的构造速度较慢。

现在知道如何选择了吗?权衡三个因素: 查找速度, 数据量, 内存使用。

这里还有个关于hash_map和map的小故事,看看:http://dev.youkuaiyun.com/Develop/article/14/14019.shtm

4.3 如何在hash_map中加入自己定义的类型?
你只要做两件事, 定义hash函数,定义等于比较函数。下面的代码是一个例子:

-bash-2.05b$ cat my.cpp
 
#include <hash_map>
 
#include <string>
 
#include <iostream>
  
 
using namespace std;
 
//define the class
 
class ClassA{
 
public:
 
ClassA(int a):c_a(a){}
 
int getvalue()const { return c_a;}
 
void setvalue(int a){c_a;}
 
private:
 
int c_a;
 
};
  
 
//1 define the hash function
 
struct hash_A{
 
size_t operator()(const class ClassA & A)const{
 
// return hash<int>(classA.getvalue());
 
return A.getvalue();
 
}
 
};
  
 
//2 define the equal function
 
struct equal_A{
 
bool operator()(const class ClassA & a1, const class ClassA & a2)const{
 
return a1.getvalue() == a2.getvalue();
 
}
 
};
  
 
int main()
 
{
 
hash_map<ClassA, string, hash_A, equal_A> hmap;
 
ClassA a1(12);
 
hmap[a1]="I am 12";
 
ClassA a2(198877);
 
hmap[a2]="I am 198877";
  
 
cout<<hmap[a1]<<endl;
 
cout<<hmap[a2]<<endl;
 
return 0;
 
}

-bash-2.05b$ make my
 
c++ -O -pipe -march=pentiumpro my.cpp -o my
 
-bash-2.05b$ ./my
 
I am 12
 
I am 198877

如何用hash_map替换程序中已有的map容器?
这个很容易,但需要你有良好的编程风格。建议你尽量使用typedef来定义你的类型:

typedef map<Key, Value> KeyMap;
当你希望使用hash_map来替换的时候,只需要修改:

typedef hash_map<Key, Value> KeyMap;
其他的基本不变。当然,你需要注意是否有Key类型的hash函数和比较函数。

    为什么hash_map不是标准的?
具体为什么不是标准的,我也不清楚,有个解释说在STL加入标准C++之时,hash_map系列当时还没有完全实现,以后应该会成为标准。如果谁知道更合理的解释,也希望告诉我。但我想表达的是,正是因为hash_map不是标准的,所以许多平台上安装了g++编译器,不一定有hash_map的实现。我就遇到了这样的例子。因此在使用这些非标准库的时候,一定要事先测试。另外,如果考虑到平台移植,还是少用为佳。

讲述一个使用hash_map的简单例子

#if defined(__GNUC__)
 
#if __GNUC__ < 3 && __GNUC__ >= 2 && __GNUC_MINOR__ >= 95
 
#include <hash_map>
 
#elif __GNUC__ >= 3
 
#include <ext/hash_map>
 
using namespace __gnu_cxx;
 
#else
 
#include <hash_map.h>
 
#endif
 
#elif defined(__MSVC_VER__)
 
#if __MSVC_VER__ >= 7
 
#include <hash_map>
 
#else
 
#error "std::hash_map is not available with this compiler"
 
#endif
 
#elif defined(__sgi__)
 
#include <hash_map>
 
#else
 
#error "std::hash_map is not available with this compiler"
 
#endif
 
#include <string>
 
#include <iostream>
 
#include <algorithm>
 
using namespace std;
 
struct str_hash{
 
size_t operator()(const string& str) const
 
{
 
return __stl_hash_string(str.c_str());
 
}
 
};
 
struct str_equal{
 
bool operator()(const string& s1,const string& s2) const {
 
return s1==s2;
 
}
 
};
  
 
int main(int argc, char *argv[])
 
{
 
hash_map<string,string,str_hash,str_equal> mymap;
 
mymap.insert(pair<string,string>("hcq","20"));
 
mymap["sgx"]="24";
 
mymap["sb"]="23";
 
cout<<mymap["sb"]<<endl;
 
if(mymap.find("hcq")!=mymap.end())
 
cout<<mymap["hcq"]<<endl;
 
return 0;
 
}
 

GK2302量产工具下载方法,GK2302开卡教程,GK2301LT怎么开卡方法,附GK2302开卡工具,GK2301LT量产软件下载 ​每种量产软件的操作流程可能略有不同,因此必须按照要求操作。为确保安全,建议使用量产部落网下载的原版量产开卡软件。 阅读详情

相关推荐

随机向量函数链神经网络(RVFLNN)简介——附测试代码

宽度学习(Broad Learning System)原理介绍:随机向量函数链神经网络(RVFLNN) 目录宽度学习(Broad Learning System)原理介绍:随机向量函数链神经网络(RVFLNN)结构分析和训练优化 函数链神经网络(Functional-link neural network,简称FLNN)的结构如下图。 和深度神经网络结构不同的是,FLNN相当于把隐层放到了输入层中,作为增强结点(Enhancement nodes)。也可以理解成FLNN在输入层就对输入向量进行了非线性变换

weixin_44517742的博客 1万+

C++中的Hashmap

C++中的Hashmap Hashmap 结构 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长。 HashMap是非线程安全的,只是用于单线程环境下,但是Hash Table是线程安全的(https://www.cnblogs.com/williamjie/p/9099141.html) 哈希表(key,value) 是把key值通过一个固定的算法即哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取

Misty_Rain_的博客 3万+

ZYNQ Linux 应用层 利用 AXI DMA 进行数据传输

ZYNQ Linux应用层 利用 AXI DMA 进行数据传输 软件版本: VIVADO2018.2 操作系统: Debian 9 硬件平台: ZYNQ-MZ7100FA 嵌入式操作系统:Debian(4.19.0-xilinx) AXI-DMA驱动:GitHub:bperez77/xilinx_axidma 一、搭建硬件环境 参见:《ZYNQ 修炼秘籍裸机篇 2019 版》第21章:CH21 利用 AXI DMA 进行批量数据环路测试 参考文章: ZYNQ跑系统 系列(四) AXI-DMA的linux下运

jin787730090的博客 1万+

c++中的hash_map

c++中的hash_maphash相关哈希函数哈希表c++中的hash_map 最近刷左神算法,hash_map经常被用到(hash_map的增删改查时间复杂度都能低至O(1))。但是左神用的都是java中的hash_map,苦于这个原因,只能自己研究一下C++中的hash_map。 参考:https://blog.youkuaiyun.com/u010025211/article/details/46653519?utm_medium=distribute.pc_relevant_bbs_down.none-task

qq_36276431的博客 6222

python采集keep运动数据

我国2009年确立“全民健身日”;2012年,“广泛开展全民健身运动”写入十八大报告;到2014年上升为国家战略,全民健身的分量越来越重。推动全民健身和全民健康深度融合,通过全民健身实现全民健康。 全民健身运动在我国越来越普及,每天清晨和傍晚时分,城市的广场上、公园里,到处是运动健身的人们,跑步、打球、跳舞、游泳、打太极拳等运动项目十分丰富,人们在运动中获得越来越多的幸福感。 随之诞生出了很多运动app,例如keep,咪咕,微信等,大家可以通过app随时记录自己的运动数据,特别是对于很多的减肥人群来说,通过

Z_suger7的博客 5657

Spring Boot日志可视化实战:5分钟搞定Promtail采集与Grafana查询(避坑指南)

本文详细介绍了如何通过Promtail和Grafana实现Spring Boot日志的可视化查询,提供从日志采集到高效查询的全流程实战指南,重点解决部署中的常见问题。内容包括Logback配置、Promtail日志采集、Grafana的LogQL查询技巧及生产环境优化建议,帮助开发者快速定位和解决日志问题。

weixin_42526015的博客 107

C++:Hash_map(哈希映射)

猎豹网校的图 ###目录###0 为什么需要hash_map1 数据结构:hash_map原理2 hash_map 使用2.1 一个简单实例2.2 hash_map 的hash函数2.3 hash_map 的比较函数2.4 hash_map 函数3 相关hash容器4 其他4.1 hash_map和map的区别在哪里?4.2 什么时候需要用hash_map,什么时候需要用map?4.3 如何...

杨杨子江的博客 4287

Matlab生成dsp程序——环境搭建(c2000系列[F28335、F2837x]都适用)

一、环境配置 (28377D的操作是一模一样就是硬件设置的时候需要选择28377d进行相应的设置) 1)matlab与ccs联合编译需要安装对应的matlab版本和ccs版本,且只有2016a以上的matlab支持c2000系列。具体对应关系参照: http://software-dl.ti.com/ccs/esd/documents/ccs_matlab.html 2)matlab版本确定之后,还需要安装有关于ccs的相关软件: I、TI Code Composer Studio xxxx(对应

qq_42151264的博客 2万+

c++ HashMap

HashMap map用法总结 hashmap简单介绍 #include< map> 数据结构 基于哈希表,以空间换时间,是存储key-value键值对的集合。 基本原理:使用一个下标范围比较大的数组来存储元素。可以设计一个函数(哈希函数,也叫做散列函数),使得每个元素的关键字都与一个函数值(即数组下标,hash值)相对应,于是用这个数组单元来存储这个元素;也可以简单的理解为,按照关键...

有趣的人 5342

BMD101传感器协议解析与STM32实现(LCD显示波形)

1 前言 BMD101传感器是神念科技开发的心电传感器。目前有一些商家开发了BMD101传感器模块,基本上都是利用蓝牙直接传输数据到PC或手机端。而本文介绍通过串口将数据传送给单片机,并在单片机上完成数据解析和心电波形LCD显示的功能。 2 通信协议解读 BMD101 通过 UART 接口通信。这是一个标准的 UART 接口,定义为 1 个起始位,8 个数据位,1 停止位格式,波特率 57600。 串口输出的数据包格式如下: 包括Header(帧头)、data payload(数据有效载荷)、CRC校验

weixin_44509533的博客 9289

Arcgis中的模型构建器(ModelBuilder)技术之批量图层转KML

这段时间全省各地都在开展耕地流出图斑整改恢复工作,期间或多或少都需要村委会协助摸排举证,除了举证软件外,奥维、外业精灵等软件想必大家接触得也很多。这就经常需要我们转kmz文件给业主或者摸排人员,那么有没有什么方法在arcgis下快速批量转kmz文件呢,今天就介绍一下如何在模型构建器(ModelBuilder)构建批量图层转KML的模型。

weixin_44671081的博客 1325

编程参考 - C++使用hashmap

我们一般使用int和string类型,是有这个运算符的,如果使用其他的类型,就可能需要自己实现operator

guoqx的专栏 3676

H3LIS200DL三轴加速度传感器原理图设计,已量产(加速度传感器)

在最近的工业振动监测项目里,我们选了 ST 的 H3LIS200DL 做核心传感器 —— 这颗料的特点太契合场景了:±100g/200g 的大量程刚好覆盖设备冲击与振动的测量需求,10μA 的超低功耗能适配电池供电的边缘节点,0.5Hz 到 1kHz 的输出速率也足够抓准动态信号。下面结合实际画的原理图,拆解下这个硬件电路的设计思路。

不脱发的程序猿 337

C++中的hash map和map的区别

C++中的hash map和map的区别

qq_44910516的博客 4152

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

ARM7 VIC中断控制器实战配置与避坑指南

中断控制器是嵌入式系统实现实时响应的核心硬件模块,其原理涉及中断请求识别、优先级仲裁、向量地址生成与硬件抢占机制。ARM7 VIC作为经典向量中断控制器,以4级硬件优先级、低延迟跳转和精简寄存器设计,在工业控制等资源受限场景中具备独特技术价值。相比软件轮询或简易中断模块,VIC可将中断响应延迟压缩至20个时钟周期内,并支持零开销中断嵌套,显著提升系统确定性。本文聚焦ARM7平台(如LPC21xx、AT91SAM7S)的VIC实战配置,涵盖向量表RAM搬移、IRQ编号映射、优先级科学分配及ISR退出规范等关键

weixin_34008933的博客 275

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++ HashMap和Map的区别

今天想查一下c++ hashmap的使用方法,搜出来的一些文章实在辣眼睛,竟然很多都混淆了c++中map和hashmap的区别。 首先,c++ 标准库的std::map内部是排序的,内部使用的是红黑树实现,不管是增加还是查找的时间复杂度 O(logN)O(logN)O(logN)。 而c++ 标准库的hashmap其实叫作std::unordered_map,其增加和查询的时间复杂度才是 O(1)...

菇星獨行 5194

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

哈希表的定义和应用

(Hash table,也叫哈希表),是根据键(Key)而直接访问在内存存储位置的数据结构。也就是说,它通过计算一个关于键值的函数,将所需查询的数据映射到表中一个位置来访问记录,这加快了查找速度。这个映射函数称做散列函数,存放记录的数组称做散列表。

m0_74724723的博客 1108

【C++高阶】哈希函数底层原理全面探索和深度解析

本文旨在揭开哈希算法的神秘面纱,带领大家深入探测哈希的底层世界。从哈希函数的内部逻辑出发,探讨其如何通过一系列的数学变换与位操作,将任意长度的输入数据映射为固定长度的输出值。同时逐步探索哈希表的数据结构冲突解决策略以及内存管理技巧。通过详细剖析哈希函数的内部逻辑与实现方式,我们将揭示那些隐藏在高效与安全背后的智慧与努力return//转成数字,把key// 特化template<>{ // 避免因为顺序不一样而产生一样的值 BKDR// 避免 abc,acb同值不同意hash *= 31。

island1314的博客 1806
上一篇: FreeCAD文档
下一篇: Qt Examples之ramebufferobject工程说明
荆楚闲人
博客等级 码龄20年 3万+粉丝 473原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值