map這個小妖精(*/ω\*)

map這個小妖精(*/ω\*)


1. map是蝦米咩

redis曉得咩,就是那個key-value結構的數據庫,map這傢伙其實就是key-value結構的容器。容器是蝦米咩,還好之前看過有關的一丁點,曉得C++裏有這個一種類存在,它的名字就叫做容~器~類~~~網上說,容器就是用來存儲數據的咩。map是標準STL關聯容器,反正有概念就好得了咩,不必太糾結了o( ̄ヘ ̄o#)


2. 它是誰家小妖精咩

來來來,讓俺們看看,這小妖精的頭文件是哪個咩

#include <map>

既然是map,當然是map家的,有木有猜到咩哈哈哈哈


3. 小妖精長什麼樣子咩

map<類型1, 類型2> 變量名稱

比如醬紫咩

map<string, string> key_value;


4. 小妖精有蝦米本領咩

4.1 賦值

map變量有了,俺要給它賦值咩,腫麼辦呢

聽說map有個叫做insert的方法闊以插入數據,插插插

噫,有種污了的錯覺絕不是俺的錯(*/ω\*),除了使用insert外當然可以它還可以像數組那樣通過下標賦值,比如醬紫:map[key] = value

下面是例子咩

#include <iostream>
#include <string>
#include <map>
using namespace std;
 
int main(int argc, char **argv)
{
    map<string, string> key_value;
    
    key_value.insert(pair<string, string>("one", "yes_one")); //插插插\(≧▽≦)/
    key_value["two"] = "yes_two"; //下標賦值咩O(∩_∩)O~~
                                            
    return 0;
}


4.2 iterator

cplusplus說map家的成員變量有醬醬釀釀的,但俺不認識,俺只認識iterator,所以就說說iterator咩~

iterator這傢伙有兩個成員變量,一個叫first,存儲map的key,一個叫second,存儲map的value

而且使用iterator就闊以遍歷map曉得咩,就像醬紫滴

#include <iostream>
#include <string>
#include <map>
using namespace std;
            
int main(int argc, char **argv)
{           
    map<string, string> key_value;
    map<string, string>::iterator iter;
            
    key_value.insert(pair<string, string>("one", "yes_one"));
    key_value["two"] = "yes_two";
            
    for(iter=key_value.begin(); iter!=key_value.end(); iter++) { //遍歷看這裏咩\(≧▽≦)/
        cout<<iter->first<<"->"<<iter->second<<endl;  //firsh和second誒         
    }       
            
    return 0;
}


4.3 查找

俺啾一下扔了兩個數據進map裏了,可俺想找到這兩個數據腫麼辦哦QAQ,因爲map是key-value結構,當然是通過key來尋找了,map有個小觸手叫做find~find~喲

調用find方法,會返回iterator。

如果find木有找到,則iterator等於map.end(),不要在這個時候試圖打印iterator的key和value,因爲會發生段錯誤,雖然俺知道會是這個結果,但俺還是閒得去嘗試了%>_<%

#include <iostream>
#include <string>
#include <map>  
using namespace std;
            
int main(int argc, char **argv)
{           
    map<string, string> key_value;
    map<string, string>::iterator iter;
            
    key_value.insert(pair<string, string>("one", "yes_one"));
    key_value["two"] = "yes_two";
    iter = key_value.find("one");  //看這裏find小觸手
            
    if (iter == key_value.end()) {
        cout<<"iter == key_value.end()\n";
        return -1;
    }       
            
    cout<<"iter->one = "<<iter->first<<endl;  
    cout<<"iter->two = "<<iter->second<<endl; 
            
    return 0;
}


4.4 刪除

因爲作死去打印不存在的key和value,俺如願以償(才木有哼哼)地dump了,所以俺很生氣後果很嚴重,俺要把插進去的數據揪出來扔掉T_T

刪除數據闊以用erase,清空數據闊以用clear

#include <iostream>
#include <string>
#include <map>  
using namespace std;
         
int main(int argc, char **argv)
{        
    map<string, string> key_value;
    map<string, string>::iterator iter;
         
    key_value.insert(pair<string, string>("one", "yes_one"));
    key_value["two"] = "yes_two";
    key_value["three"] = "yes_three";
    for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
        cout<<iter->first<<"->"<<iter->second<<endl;
    }   
    cout<<endl;
        
    key_value.erase("two"); //看這裏看着裏咩
    for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
        cout<<iter->first<<"->"<<iter->second<<endl;
    }   
    cout<<endl;
        
    key_value.clear(); //清空清空清空
    for (iter=key_value.begin(); iter!=key_value.end(); iter++) {
        cout<<iter->first<<"->"<<iter->second<<endl;
    }   
    cout<<endl;                                                           
         
    return 0;
} 

/*
運行結果:
one->yes_one
three->yes_three
two->yes_two

one->yes_one
three->yes_three


*/


噫,又寫了一堆廢話,不曉得未來的俺看到後會不會有種想掐死俺的衝動咩哈哈哈

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