STL中map,set的使用

Map:
1.定義:
Map是標準關聯式容器之一,一個map是一個鍵值對序列,即(key ,value)對。它提供基於key的快速檢索能力,在一個map中key值是唯一的。

2.Map常用的方法主要有:insert,erase,size,count,begin,end,find,clear,empty。

insert方法:在map中插入一個元素,map中記錄的元素通常爲鍵值對,所以,在存儲時會把,鍵和值封裝成pair然後進行插入,例如:mymap.insert(pair < string,string>(str,num));其中str和num爲string類型的變量。當然也可以簡單的寫成mymap[str]=num;此處mymap即爲map<string,string>類型的變量。因爲map在實現過程中對[]進行了重載。
第一種方式若插入的元素的鍵值已經存在於map中,那麼就會插入失敗,不會修改元素的鍵值對信息,若鍵值在map中查找不到,那麼就會將該新元素加入到map中去。
第二種方式比較直觀,但存在一個性能的問題。插入時,先在mymap中查找主鍵爲str的項,沒發現,然後將一個新的對象插入mymap,鍵是str,值是一個空字符串,插入完成後,將字符串賦爲num, 該方法會將每個值都賦爲缺省值,然後再賦爲顯示的值,如果元素是類對象,則開銷比較大。若找到鍵值爲str的項,則用num更改原來的num值。

erase方法:
erase主要是刪除map中的某個項,需要參數key,例如mymap.erase(str);此句的意思就是刪除key值爲str的鍵值對。

size方法:
統計map中鍵值對的個數,mymap.size()返回值即爲mymap中鍵值對的個數,若map爲空則返回0。

count方法:
統計map中某個鍵值出現的次數,因爲map中鍵值唯一,所以此方法可以用來檢測某鍵值是否存在,例如在刪除時可以mymap.count(str),若爲0則可以提示用戶此鍵值不存在,若爲1則直接刪除。

begin、end方法:
begin方法返回map迭代器類型,通過此迭代器與end方法的返回值進行比較就可以很容易的對map進行遍歷。

find方法:
查找某個鍵在map中的位置。

clear方法:
清空map中的所有元素

empty方法:
判斷map是否爲空,若爲空則返回真若非空則返回假。

用例代碼如下:

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main()
{
    map<string,int> mymap;
    map<string,int>::iterator it;
    //插入
    mymap.insert ( pair<string,int>("string",1) );
    mymap.insert ( pair<string,int>("sort",2) );
    mymap.insert ( pair<string,int>("erase",3) );
    mymap.insert ( pair<string,int>("begin",4) );
    mymap["find"] = 5;

    mymap.erase("find");//刪除

    it = mymap.find("begin");
    mymap.erase(it);
    cout<<"map中鍵隊的個數:"<<mymap.size()<<endl;
    cout<<"map是否爲空?"<<mymap.empty()<<endl;

    for ( it=mymap.begin() ; it != mymap.end(); it++ )
    {
        cout << (*it).first << " => " << (*it).second << endl;
    }

    mymap.clear();//全刪除
    cout<<"map是否爲空?"<<mymap.empty()<<endl;

    for ( it=mymap.begin() ; it != mymap.end(); it++ )
    {
        cout << (*it).first << " => " << (*it).second << endl;
    }
    return 0;
}

結果:
這裏寫圖片描述

set的用法:
1.定義:
set(集合)是一個容器,它其中所包含的元素的值是唯一的。set支持唯一鍵值,set中的值都是特定的,而且只出現一次。
2.set常用的主要方法有:insert,erase,find,count,size,begin,end等。

1、插入:insert()
1)pair<iterator,bool> insert ( const value_type& x )
在迭代器中插入一個數據,如果這個數據不存在就直接插入,其返回值爲插入後元素的迭代器和true。如果這個元素已經存在,那麼返回當前元素以及false.
2)iterator insert ( iterator position, const value_type& x )
在指定的位置插入指定的數據,position是一個迭代器的位置,x表示的是要插入的數。如果插入成功的話,會返回一個插入新位置的迭代器。
3)template<class InputIterator> void insert(InputIterator first,InputIterator last)
插入一段迭代器區間
**2、刪除(erase)
1)void erase(iterator position)**
刪除一個迭代器位置
2)size_type erase(sonst key_type& x)
刪除成功的話就返回1
3)void erase(iterator first, iterator last)
刪除一段迭代器區間
3、查找:find()
iterator find(const key_type& x) const;
若找到返回該迭代器的位置,否則就返回的是最後一個元素後面的位置
4、數量:count()
**size_type count(const key_type& x)con**st
count是爲了統計某個數值出現的次數,在set裏面不能重複出現相同的數值,count的返回值只有0和1兩個值,0表示的是不存在,1表示存在。

用例代碼如下:


#include<iostream>  
using namespace std;  
#include<set>  
void fun(set<int> myset)    
{    
    set<int>::iterator it;    
    for (it=myset.begin(); it!=myset.end(); it++)    
    {    
        cout <<*it<<" ";    
    }    
    cout << endl;    

}    
void TestSet()    
{    
    set<int> t;    
    set<int>::iterator it;    
    pair<set<int>::iterator,bool> ret; 

    //第一種插入方法
    cout<<"插入1,2,3,4,5:"<<endl;
    t.insert(1);    
    t.insert(2);    
    t.insert(3);    
    t.insert(4);    
    t.insert(5);    
    fun(t);    
    cout<<endl;    

    cout<<"再次插入2:"<<endl;
    ret = t.insert(2);  
    if(ret.second == false)  
    {    
        it=ret.first;      
    }    
    cout<<*it<<endl;    

    //第二種插入 ,在某一位置插入 
    cout<<"在某一位置插入7,8,9:"<<endl;
    t.insert (it,7);                     
    t.insert (it,8);                     
    t.insert (it,9);                     
    fun(t);    
    cout<<endl;    

    //第三種插入  ,將某個區間插入  
    cout<<"將一個數組插入:"<<endl;
    int a[]= {10,11,12};                 
    t.insert (a,a+3);    
    fun(t);    
    cout<<endl;

    cout<<"10是否存在:"<<endl;
    cout<<"10 count:"<<t.count(10)<<endl;    
    cout<<"size:"<<t.size()<<endl;    

    cout<<"刪除1:"<<endl;
    t.erase (1);  
    fun(t);    
    cout<<endl;    

    cout<<"刪除3:"<<endl;
    it=t.begin();    
    it++;                                                
    t.erase (it);
    fun(t);    
    cout<<endl;  

    cout<<"刪除11及以後的數:"<<endl;
    it=t.find (11);    
    t.erase ( it, t.end() );  
    fun(t);    
    cout<<endl;    
}  

int main()  
{  
    TestSet();  
    return 0;  
}  

結果如下:
這裏寫圖片描述

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