第二章 C++ STL 泛型編程 3

map映照容器

map也是用紅黑樹實現的,插入元素的鍵值不允許重復,比較函數針對鍵值比較。

map函數

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

int main()
{
    map<string, float> m;
    m["jack"] = 98;
    m["bomi"] = 96;
    m["kate"] = 97;

    map<string, float>::iterator it;
    for (it = m.begin(); it != m.end(); it ++)
        cout << (*it).first<<":"<<(*it).second<<endl;
    return 0;
}

元素的增刪查

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

int main()
{
    map<int,char> m;
    //插入元素,按鍵值的由小到大放入黑白樹中
    m[25]='m';
    m[28]='k';
    m[10]='x';
    m[30]='a';

    map<int,char>::iterator it;
    for (it = m.begin(); it != m.end(); it ++)
        cout << (*it).first<<":"<<(*it).second<<endl;

    it = m.find(28);
    if(it!=m.end())//搜索到該鍵值
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    else
        cout<<"not found it"<<endl;

    //刪除鍵值為 28 的元素
    m.erase(28);
    //反向遍歷元素
    map<int,char>::reverse_iterator rit;
    for(rit = m.rbegin(); rit != m.rend(); rit ++)
        cout<<(*rit).first<<" : "<<(*rit).second<<endl;

    return 0;
}

自定義比較函數,與set一樣的兩種方式:

  1. 如果元素不是結構體,可以編寫比較函數。按鍵值由大到小的順序將元素插入到 map 中:
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

//自定義比較函數
struct cmp
{
    bool operator()(const int &a,const int &b)
    {
        if(a!=b)
            return a>b;
        else
            return a>b;
    }
};

int main()
{
    map<int, char, cmp> m;
    m[25]='m';
    m[28]='k';
    m[10]='x';
    m[30]='a';

    map<int, char, cmp>::iterator it;
    for(it = m.begin();it != m.end(); it ++)
        cout<<(*it).first<<" : "<<(*it).second<<endl;

    return 0;
}
  1. 如果元素是結構體,,直接把比較函數寫在結構體內
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

struct Info
{
    string name;
    float score;
    //重載“<”操作符,自定義排序規則
    bool operator < (const Info &a) const
    {
        //按 score 由大到小排列。如果要由小到大排列,使用“>”號即可
        return a.score < score;
    }

};

int main()
{
    map<Info,int> m;
    Info info;
    //插入元素,按鍵值的由小到大放入黑白樹中
    info.name = "Jack";
    info.score = 60;
    m[info] = 25;
    info.name = "Bomi";
    info.score = 80;
    m[info] = 10;
    info.name = "Peti";
    info.score = 66.5;
    m[info] = 30;

    map<Info,int>::iterator it;
    for(it=m.begin();it!=m.end();it++)
    {
        cout<<(*it).second<<" : ";
        cout<<((*it).first).name<<" "<<((*it).first).score<<endl;
    }

    return 0;
}

用map實現數字分離

把數字當成字符串,使用 map 的映照功能,很方便地實現了數字分離。

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

int main()
{
    map<int,char> m;
    //字符映射數字
    for(int j = 0; j < 10; j ++)
        m['0' + j] = j;

    string sa,sb;
    sa="6234";
    int i;
    int sum=0;
    for(i = 0; i <sa.length(); i ++)
    {
        sum += m[sa[i]];
    }
    cout<<"sum = "<<sum<<endl;

    //數字映照字符
    for(int j = 0;j < 10;j ++)
    {
        m[j]='0'+j;
    }
    int n = 7;
    string s ="The number is ";
    cout<<s + m[n]<<endl;

    return 0;
}
=>
sum = 15
The number is 7

multimap多重映照容器

multimap允許插入重復鍵值元素。

multimap函數

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

int main()
{
    multimap<string,double> m;
    //插入元素
    m.insert( pair<string,double>("Jack",300.5) );
    m.insert( pair<string,double>("Kity",200) );
    m.insert( pair<string,double>("Memi",500) );
    //重復插入鍵值“Jack”
    m.insert( pair<string,double>("Jack",306) );
    //使用前向迭代器中序遍歷 multimap
    multimap<string,double>::iterator it;
    for(it = m.begin(); it != m.end(); it ++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

    //查找鍵值
    cout<<endl<<"the searching result:"<<endl;
    it=m.find("Jack");
    if(it!=m.end())//找到
    {
        cout<<(*it).first<<" "<<(*it).second<<endl;
    }
    else//沒找到
    {
        cout<<"not find it"<<endl;
    }

    it=m.find("Nacy");
    if(it!=m.end())//找到
    {
        cout<<(*it).first<<" "<<(*it).second<<endl;
    }
    else//沒找到
    {
        cout<<"not find it"<<endl;
    }

    cout<<endl;
    //刪除鍵值等于“Jack”的元素
    m.erase("Jack");
    //使用前向迭代器中序遍歷 multimap
    cout<<"the elements after deleted:"<<endl;
    for(it=m.begin();it!=m.end();it++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

    return 0;
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容