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的區別

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章