zoukankan      html  css  js  c++  java
  • 置顶结构[置顶] C++中hash map的用法

    查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!

    
    

        大家都知道c++ stl中的map是用红黑树实现的,而tr1中实现了hash结构的map 为了不定名冲突被叫做了unordered_map,,

        上面就给大家简略应用下这一结构,如果大家的结构在百万规模级就应该考虑应用该结构了。

        每日一道理
    曾经辉煌过,曾经凋零过,这可是你至死不渝的生活吗?我亲爱的母亲—大自然。多少次,我伏在地上,去聆听你沉重的脉搏声;多少次,我伫立在山前,去感受那松涛千年的浩瀚。你的豪壮,足以让中华民族腾飞;你的无私,谱写了一曲曲感人至深的千古壮曲。
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <unordered_map>
    using namespace std;
    
    void print(std::pair<int,std::string> it)
    {
    	cout << it.first << "-> " << it.second << endl;
    }
    
    void print2(std::pair<string,std::string> it)
    {
    	cout << it.first << "-> " << it.second << endl;
    }
    
    
    int main(int argc, char* argv[])
    {
    	typedef std::tr1::unordered_map<int,string> hash_map;
    	hash_map hm;
    	hm.insert(std::pair<int,std::string>(0,"Hello"));
    	hm[1] = "World";
    	for_each(hm.begin(),hm.end(),print);
    
    
    	std::tr1::unordered_map<string,string> hashcode_string;
    	hashcode_string.insert(std::pair<string,std::string>("opensss","release"));
    
    	hashcode_string["opensde"] = "ssss";
    	for_each(hashcode_string.begin(),hashcode_string.end(),print2);
    
    	system("pause");
    	return 0;
    }

    文章结束给大家分享下程序员的一些笑话语录: IBM和波音777
      波音777是有史以来第一架完全在电脑虚拟现实中设计制造的飞机,所用的设备完全由IBM公司所提供。试飞前,波音公司的总裁非常热情的邀请IBM的技术主管去参加试飞,可那位主管却说道:“啊,非常荣幸,可惜那天是我妻子的生日,So..”..
      波音公司的总载一听就生气了:“胆小鬼,我还没告诉你试飞的日期呢!”

    --------------------------------- 原创文章 By
    置顶和结构
    ---------------------------------

  • 相关阅读:
    React简明学习
    react-router简明学习
    react组件生命周期
    在vue中使用css modules替代scroped
    深入理解javascript中的事件循环event-loop
    javascript中的内存管理和垃圾回收
    移动端中的陀螺仪
    基于create-react-app的再配置
    vscode常用设置
    更高效地使用搜索引擎
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3111300.html
Copyright © 2011-2022 走看看