STL---map

來源:互聯網
上載者:User

標籤:algorithm   span   fun   std   play   \n   begin   splay   first   

#include<iostream>#include<map>#include<algorithm>using namespace std;void func(pair<int, char> t){    cout << "key: " << t.first << "  value: " << t.second << "\n";}int main(){    map<int, char> mp;  //無參構造  第一個參數是索引值(不能重複),第二個參數可以重複    mp.insert(pair<int, char>(1, ‘a‘));    //pair 是stl為我們寫好的一個結構體,照著來就是了,但是這個pair的使用看起來有點繁瑣,我們可以使用typedef重新命名一下    typedef pair<int, char>  in_pair;    mp.insert(in_pair(2, ‘b‘));    for_each(mp.begin(), mp.end(), func);    //當我們重複插入相同的key的時候,不會報錯,但是會返會一個特殊類型    pair<map<int, char>::iterator, bool> pr;    pr = mp.insert(in_pair(3, ‘c‘));    cout << "是否插入成功 " << pr.second << "\n";  //輸出這個特殊結構的第二個bool類型    pr = mp.insert(in_pair(3, ‘c‘));    cout << "是否插入成功 " << pr.second << "\n";    return 0;}
View Code
#include<iostream>#include<map>#include<algorithm>using namespace std;void func(pair<int, char> t){    cout << "key: " << t.first << "  value: " << t.second << "\n";}int main(){    //map的底層實現是紅/黑樹狀結構,每一次插入都會引起排序,尋找某一個元素的代價是Log(n)    typedef pair<int, char>  in_pair;    map<int, char> mp;    mp.insert(in_pair(1, ‘a‘));    mp.insert(in_pair(2, ‘b‘));    mp.insert(in_pair(5, ‘e‘));    mp.insert(in_pair(4, ‘d‘));    mp.insert(in_pair(3, ‘c‘));    for_each(mp.begin(), mp.end(), func);    map<int, char> mp1;    mp1.insert(in_pair(6, ‘f‘));    mp1.insert(mp.begin(), mp.end());  //使用另一個map來進行插入    cout << "*****************************************\n";    for_each(mp1.begin(), mp1.end(), func);    cout << "***************************************\n";    map<int, char> mp2(mp);  //建構函式2    for_each(mp2.begin(), mp2.end(), func);    cout << "***************************************\n";    map<int, char> mp3(mp1.begin(), mp1.end());  //建構函式3    for_each(mp3.begin(), mp3.end(), func);    return 0;}
View Code

 

STL---map

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.