1.简介

  随着C++0x标准的确立,C++的标准库中也终于有了hash table这个东西。很久以来,STL中都只提供<map>作为存放对应关系的容器,内部通常用红黑树实现,据说原因是二叉平衡树(如红黑树)的各种操作,插入、删除、查找等,都是稳定的时间复杂度,即O(log n);但是对于hash表来说,由于无法避免re-hash所带来的性能问题,即使大多数情况下hash表的性能非常好,但是re-hash所带来的不稳定性在当时是不能容忍的。不过由于hash表的性能优势,它的使用面还是很广的,于是第三方的类库基本都提供了支持,比如MSVC中的<hash_map>和Boost中的<boost/unordered_map.hpp>。后来Boost的unordered_map被吸纳进了TR1 (C++ Technical Report 1),然后在C++0x中被最终定了标准。

2.示例代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std; void main(){
unordered_map<string,int> months;
//插入数据
cout<<"insert data"<<endl;
months["january"]=;
months["february"] = ;
months["march"] = ;
months["september"] = ; //直接使用key值访问键值对,如果没有访问到,返回0
cout<<"september->"<<months["september"]<<endl;
cout<<"xx->"<<months["xx"]<<endl; typedef unordered_map<int,int> mymap;
mymap mapping;
mymap::iterator it;
mapping[]=;
mapping[]=;
const int x=;const int y=;//const是一个C语言(ANSI C)的关键字,它限定一个变量不允许被改变,产生静态作用,提高安全性。 //寻找是否存在键值对
//方法一:
cout<<"find data where key=2"<<endl;
if( mapping.find(x)!=mapping.end() ){//找到key值为2的键值对
cout<<"get data where key=2! and data="<<mapping[x]<<endl;
}
cout<<"find data where key=3"<<endl;
if( mapping.find(y)!=mapping.end() ){//找到key值为3的键值对
cout<<"get data where key=3!"<<endl;
}
//方法二:
it=mapping.find(x);
printf("find data where key=2 ? %d\n",(it==mapping.end()));
it=mapping.find(y);
printf("find data where key=3 ? %d\n",(it==mapping.end())); //遍历hash table
for( mymap::iterator iter=mapping.begin();iter!=mapping.end();iter++ ){
cout<<"key="<<iter->first<<" and value="<<iter->second<<endl;
} system("pause");
}

C++中的unordered_map的更多相关文章

  1. C++11中std::unordered_map的使用

    unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...

  2. unordered_map 与 map 的对比(转)

    unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value.不同的是unordered_map不会根据key的大小进行排序, 存储时是根据key的ha ...

  3. C++ STL之unordered_map和unordered_set的使⽤

    写在最前面,本文摘录于柳神笔记: unordered_map 在头⽂件 #include <unordered_map> 中, unordered_set 在头⽂件 #include &l ...

  4. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  5. STL中的隐性性能开销与副作用

    1 隐性性能开销 1.1 STL容器的clear的时间复杂度不是O(1) 很多人潜意识认为STL容器中clear()成员函数的时间复杂度为常量时间复杂度O(1).原因是大家觉得对于vector而言,c ...

  6. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  7. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  8. Java - 容器详解

    一.ArrayList 长度可变数组,类似于c++ STL中的vector. 元素以线性方式连续存储,内部允许存放重复元素. 允许对元素进行随机的快速访问,但是向ArrayList中插入和删除元素的速 ...

  9. [cocos2d-x]深入--几个代表性的类 (续)

    摘要: 此文对cocos2d-x引擎中最具代表性,最能体现框架结构的几个类做了简单的介绍, 包括Director,Application, Renderer, EventDispatcher, Sch ...

随机推荐

  1. dev gridcontrol 单箱效果

    private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChan ...

  2. 第六章SignalR的服务器广播

    第六章SignalR的服务器广播 1.概述: VS可以通过 Microsoft.AspNet.SignalR.Sample NuGet包来安装一个简单的模拟股票行情应用.在本教程的第一部分,您将从头开 ...

  3. SQL游标+递归查询客户子客户转换率

    ALTER PROCEDURE [dbo].[Account3YearsConversion ] as DECLARE @AccountId UNIQUEIDENTIFIER , @yearbefor ...

  4. CentOS 7解决Local Time与实际时间相差8小时问题

    通过date -s “2014-12-06 15:00:00”以及timedatectl set-time “2014-12-06 15:00:00” ,以及ntp等方式均知识临时有效,苦恼了我半天. ...

  5. 斐波那契数列 的两种实现方式(Java)

    import java.util.Scanner; /* 斐波那契数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 如果设F(n)为该数列的第n ...

  6. log4j异常问题

    log4j:WARN No appenders could be found for logger   转自:最爱NBA 直接写我的解决办法:在src下面新建file名为log4j.propertie ...

  7. Maximum & Minimum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. hdu 4681 string

    字符串DP 题意:给你三个字符串a,b,c求字符串d的长度. 字符串d满足的要求:是a和b的公共子序列,c是它的子串. 定义dp1[i][j]表示a的第 i 位与b的第 j 位之前相同的子序列长度(包 ...

  9. 编程实现任意长度整数的加法(整数可以长度超出C++中int范围)

    #include <iostream> #include<string> using namespace std; string add(string s1,string s2 ...

  10. Eclipse --Type /com.xx.app/gen already exists but is not a source folde解决方案

    两种解决方案: Two actions, first: 1.Right click on the project and go to "Properties" 2.Select & ...