C++的map与hash_map

  • 先来看问题和解决方式

http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html

要求: 将mymap中itemstruct 的a大于100的项删除

struct   itemstruct { 
    int   a; 
    char   b[20]; 
};
map< string, itemstruct > mymap;
  • 解答1
 #include <iostream> 
#include <ctime> 
#include <map> 
using namespace std; 
typedef struct itemstruct 
{ 
    int a; 
    char b[20]; 
}itemS; 

itemS s[4]= {{102,"what"}, {33, "hello"},{198,"world"},{45, "c++"} };; 


int main() 
{ 
    map<string, itemS> mymap; 
    string str[4] = {"1st","2nd","3rd","4th"}; 
    for(int i = 0; i<4; i++) 
    { 
        mymap.insert(make_pair(str[i], s[i])); 
    } 

    map<string,itemS>::iterator   it; 
    for(it=mymap.begin();   it!=mymap.end(); it++) 
    { 
        if(it->second.a >100){ 
            i=mymap.erase(it);  //----->正确
            mymap.erase(it);     //----->it失效..
        }
    }
    //firstKey, secondvalue;
    for(it = mymap.begin();  it!=mymap.end(); it++) 
    { 
        cout<<it->first<<"   "<<it->second.a<<"   "<<it->second.b<<endl; 
    } 
    system("pause"); 
    return   0; 
}
  • 解答2
#include<map> 
#include<iterator> 
#include<string> 
#include<iostream> 
#include<cstring> 
using   namespace   std; 
struct   itemstruct 
{ 
    int   a; 
    char   b[20]; 
    itemstruct(int   t,char*str) 
    { 
        a=t; 
        strcpy(b,str); 
    } 
}; 
int   main() 
{ 
    map<string,itemstruct>mymap; 
    mymap.insert(make_pair("a",itemstruct(10,"hanzhou"))); 
    mymap.insert(make_pair("ab",itemstruct(20,"fuzhou"))); 
    mymap.insert(make_pair("abc",itemstruct(30,"zhengzhou"))); 
    mymap.insert(make_pair("abcd",itemstruct(200,"wuhan"))); 
    mymap.insert(make_pair("abcde",itemstruct(150,"kunming"))); 
    mymap.insert(make_pair("abcdef",itemstruct(50,"xiamen"))); 
    map<string,itemstruct>::iterator   it=mymap.begin(); 
    while(it!=mymap.end()) 
    { 
        if((it->second).a>100)mymap.erase(it++); 
        else   it++; 
    } 
    it=mymap.begin(); 
    while(it!=mymap.end()) 
    { 
        cout<<it->first<<"   "<<(it->second).a<<"   "<<(it->second).b<<endl; 
        it++; 
    } 
    system("PAUSE"); 
    return   0; 
}
  • map的现实动机

http://blog.sina.com.cn/s/blog_a9303fd9010195hm.html

举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:

Map<int, string> mapStudent;
  • map的定义使用说明

1 头文件

  #include   <map> 

2 定义

  map<string,   int>   my_Map; 
  或者是typedef     map<string,   int>   MY_MAP; 
  MY_MAP   my_Map; 

3 插入数据

  (1)   my_Map["a"]   =   1; 
  (2)   my_Map.insert(map<string,   int>::value_type("b",2)); 
  (3)   my_Map.insert(pair<string,int>("c",3)); 
  (4)   my_Map.insert(make_pair<string,int>("d",4)); 

4 查找数据和修改数据

 (1)   int   i   =   my_Map["a"]; 
            my_Map["a"]   =   i; 
  (2)   MY_MAP::iterator   my_Itr; 
            my_Itr.find("b"); 
            int   j   =   my_Itr->second; 
            my_Itr->second   =   j; 

不过注意,键本身是不能被修改的,除非删除。

5 删除数据

 (1)   my_Map.erase(my_Itr); 
 (2)   my_Map.erase("c"); 

还是注意,第一种情况在迭代期间是不能被删除的,道理和foreach时不能删除元素一样。

6 迭代数据

  for   (my_Itr=my_Map.begin();   my_Itr!=my_Map.end();   ++my_Itr)   {} 

7 其它方法

  my_Map.size()               返回元素数目 
  my_Map.empty()       判断是否为空 
  my_Map.clear()           清空所有元素 
  可以直接进行赋值和比较:=,   >,   >=,   <,   <=,   !=   等等
  • 来看看hash_map作为一种比较手段

(http://blog.sina.com.cn/s/blog_7423d2260101jon5.html)

#include <hash_map>  
#include <string>  
#include <iostream> 

using namespace __gnu_cxx;
using namespace std;

struct str_hash{
    size_t operator()(const string& str) const {
        return __stl_hash_string(str.c_str());
    }
};

struct compare_str {
    bool operator()(const string& str1,const string& str2) const{
        return str1 == str2;
    }
};

int main()
{
    typedef hash_map namehash;
    namehash strhash;
    namehash::iterator it;

    //通过[]方式添加hash_map
    strhash["岳不群"]="华山派掌门人,人称君子剑";
    strhash["张三丰"]="武当掌门人,太极拳创始人";
    strhash["东方不败"]="第一高手,葵花宝典";

    //通过pair方式插入hash_map
    strhash.insert(pair("IntPassion", "林元已于,风而存在"));
    strhash.insert(make_pair("陈英俊", "玉树临风,英俊潇洒"));

    //通过find方式查看hash_map的元素
    it = strhash.find("陈英俊");
    cout<<it->first<<" -> "<<it->second<<endl;

    //通过[]方式获取hash_map元素
    cout<<"IntPassion -> "<<strhash["IntPassion"]<<endl;

    cout<<"\n遍历输出hash_map:\n";
    for(namehash::iterator itb = strhash.begin(); itb!=strhash.end();itb++)
        cout<<itb->first<<" -> "<<itb->second<<endl;

    return 0;
}
  • 再来看看原理

本质上,hash_map的动机也是为了查找,和hash一样,他的查找方式是基于转换关键字形成的链表形成的快捷查找方式。

  • 参考文献

STL hash_map使用小结

C++中map和hash_map的区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值