C++ Primer(第五版)學習筆記_8_標準模板庫_map映照容器,_8_map

來源:互聯網
上載者:User

C++ Primer(第五版)學習筆記_8_標準模板庫_map映照容器,_8_map

C++ Primer(第五版)學習筆記_8_標準模板庫_map映照容器

map映照容器的元素資料是由一個鍵值和一個映照資料群組成的,鍵值與映照資料之間具有一一映照的關係。
map映照容器的資料結構也是採用紅/黑樹狀結構來實現的。

1、map建立、元素插入和遍曆訪問

#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    map<string, float> str;    //插入元素,按索引值的由小到大放入紅/黑樹狀結構中    str["Jack"] = 98.5;    str["Bomi"] = 96.0;    str["Kate"] = 97.5;    for(map<string, float>::iterator iter = str.begin(); iter != str.end(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    return 0;}

運行結果:
Bomi : 96
Jack : 98.5
Kate : 97.5


2、刪除元素和重複插入值:erase()和clear()方法
(1)與set容器一樣,map映照容器的erase()刪除元素函數,可以刪除某個鍵值上的所有元素。也可以使用clear()方法清空map映照容器。
(2)對於重複插入的值,只取最後插入的值
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    map<int, char> str;    //插入元素,按索引值的由小到大放入紅/黑樹狀結構中    str[25] = 'm';    str[28] = 'k';    str[10] = 'x';    str[30] = 'a';    str[25] = 'i'; //重複插入的值,採用最後插入的值    str[10] = 'y'; //重複插入的值,採用最後插入的值    cout << "未經處理資料:" << endl;    for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    //刪除索引值為0的元素    int n = str.erase(0);cout << "刪除元素個數:" << n << endl;//刪除索引值為25的元素    n = str.erase(25);cout << "刪除元素個數:" << n << endl;    cout << "erase()後資料:" << endl;    for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    str.clear();    cout << "clear()後資料:" << endl;    for(map<int, char>::iterator iter = str.begin(); iter != str.end(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    return 0;}

運行結果:
未經處理資料:
10 : y
25 : i
28 : k
30 : a 
刪除元素個數:0
刪除元素個數:1
erase()後資料:
10 : y
28 : k
30 : a 
clear()後資料:


3、元素反向遍曆:reverse_iterator
需要rbegin()方法和rend方法指出反向遍曆的起始位置和終止位置。
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    map<int, char> str;    //插入元素,按索引值的由小到大放入紅/黑樹狀結構中    str[25] = 'm';    str[28] = 'k';    str[10] = 'x';    str[30] = 'a';    for(map<int, char>::reverse_iterator iter = str.rbegin(); iter != str.rend(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    return 0;}

運行結果:
30 : a 
28 : k
25 : m
10 : y


4、元素的搜尋:find()
與set一樣,使用find()方法搜尋某個鍵值,搜尋到了返回鍵值所在迭代器位置。否則,返回end()迭代器位置。由於採用紅/黑樹狀結構結構,搜尋速度是極快的。
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    map<int, char> str;    //插入元素,按索引值的由小到大放入紅/黑樹狀結構中    str[25] = 'm';    str[28] = 'k';    str[10] = 'x';    str[30] = 'a';    map<int, char>::iterator iter;    iter = str.find(28);    if(iter != str.end())        cout << (*iter).first << " : " << (*iter).second << endl;    else        cout << "not found it" << endl;    return 0;}

運行結果:
28 : k


5、自訂比較函數
預設情況下,按照鍵值由小到大的順序插入元素。由於內部資料結構都是紅/黑樹狀結構,因此編寫比較函數與set是一致的。編寫方法有兩種,
(1)如果元素不是結構體,那麼可以編寫比較函數。下面實現鍵值由大道小的順序將元素插入map中:
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;struct myComp{    bool operator()(const int& a, const int& b)    {         return a > b;    }};int main(){    //插入元素,按索引值的由大到小放入紅/黑樹狀結構中    map<int, char, myComp> str;    str[25] = 'm';    str[28] = 'k';    str[10] = 'x';    str[30] = 'a';    for(map<int, char, myComp>::iterator iter = str.begin(); iter != str.end(); iter++)        cout << (*iter).first << " : " << (*iter).second << endl;    return 0;}

運行結果:
運行結果:
30 : a 
28 : k
25 : m
10 : x


(2)如果元素是結構體,那麼,可以直接把比較函數寫在結構體裡面。
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;struct Info{    string name;    float score;    //必須要寫重載函數,重載"<"操作符,自訂定序    bool operator < (Info a) const    {        //按score由大到小排列。如果要由小到大排列,使用">"號即可;        return a.score < score;    }};int main(){    //插入元素,按索引值的由大到小放入紅/黑樹狀結構中    map<Info, int> str;    Info info;    info.name = "Jack";    info.score = 60;    str[info] = 25;    info.name = "Bomi";    info.score = 80;    str[info] = 10;    info.name = "Peti";    info.score = 66.5;    str[info] = 30;    for(map<Info, int>::iterator iter = str.begin(); iter != str.end(); iter++)    {        cout << (*iter).second << " : ";        cout << ((*iter).first).name << " " << ((*iter).first).score << endl;    }    return 0;}
運行結果:
10 : Bomi 80
30 : Peti 66.5 
25 : Jack 60 


6、字元映照數位map寫法
對數位各位進行分離,採用取餘數的數學操作是很耗時的。而把數字當成字串,使用map的映照功能,很方便地實現了數字分離。
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    //插入元素,按索引值的由大到小放入紅/黑樹狀結構中    map<char, int> str;    str['0'] = 0;    str['1'] = 1;    str['2'] = 2;    str['3'] = 3;    str['4'] = 4;    str['5'] = 5;    str['6'] = 6;    str['7'] = 7;    str['8'] = 8;    str['9'] = 9;    /*//上面指派陳述式可以用迴圈代碼簡化    for(int j = 0; j < 10; j++)        m['0' + j] = j;    */    string a;    a = "6234";    int sum = 0;    for(int i = 0; i < a.length(); i++)        sum += str[a[i]];    cout << "各個數位和為: " << sum << endl;    sum = str[a[0]];    for(int i = 1; i < a.length(); i++)    {        sum *= 10;        sum += str[a[i]];    }    cout << "字串轉化為數字: " << sum << endl;}

運行結果:
各個數位和為:15
字串轉化為數字:6234


7、數字映照字元的map寫法
#include <iostream>#include <stdio.h>#include <vector>#include <map>#include <string>using namespace std;int main(){    //插入元素,按索引值的由大到小放入紅/黑樹狀結構中    map<int, char> str;    str[0] = '0';    str[1] = '1';    str[2] = '2';    str[3] = '3';    str[4] = '4';    str[5] = '5';    str[6] = '6';    str[7] = '7';    str[8] = '8';    str[9] = '9';    /*    //上面指派陳述式可以用迴圈代碼簡化    for(int j = 0; j < 10; j++)        m[j] = '0' + j;    */    int n = 7;    string single;    cout << "單位元字轉化為字串: " << single + str[n] << endl;    int num = 6234;    string s;    for(int i = num; i != 0; i /= 10)    {        s.insert(s.begin(), str[i % 10]);    }    cout << "多位元字轉化為字串: " << s << endl;    return 0;}

運行結果:
單位元字轉化為字串:7
多位元字轉化為字串:6234

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.