国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ STL之map和multimap用法

2019-11-06 06:17:10
字體:
供稿:網(wǎng)友

一. Map與 Multimap 特點

Map 和 Multimap 將(key/value) 一組當(dāng)做元素,它們可以根據(jù) key 的排序準(zhǔn)則自動將元素排序。

Multimap允許重復(fù)元素,map不允許重復(fù)。

類型定義:

[cpp] view plaincopytypedef std::map<std::string, float, std::greater<string> > StringFloatMap;     ...  StringFloatMap coll;  

注意,兩個>之間需加上一個空格,因為>>會被編譯器視為移位操作符。

Map 和 multimap 通常以平衡二叉樹來實現(xiàn),map 和 multimap 擁有 set 和 multiset 所有能為和所有操作函數(shù)。

通常,不能直接改變key 的值,value 的值是可以直接修改的,因此可以想像元素的實質(zhì)類型是 pair<constkey, T>。

二、在map和multimap中添加元素

map<int ,string>maplive;1. maplive.insert(pair<int,string>(102,"aclive"));2. maplive.insert(map<int,string>::value_type(321,"hai"));3. maplive[112]="APRil";//map中最簡單最常用的插入添加!

4. map中元素的查找:

find()函數(shù)返回一個迭代器指向鍵值為key的元素,如果沒找到就返回指向map尾部的迭代器。

map<int ,string >::iteratorl_it;; l_it=maplive.find(112);//返回的是一個指針if(l_it==maplive.end())cout<<"we do not find112"<<endl;else

cout<<"wo find112"<<endl;

 

map<string,string>m;

if(m[112]=="")

cout<<"we do not find112"<<endl;

4. map中元素的刪除:如果刪除112;map<int ,string>::iterator l_it;l_it =maplive.find(112);if( l_it == maplive.end())cout<<"we do not find112"<<endl;else maplive.erase(l_it);//delete 112;

5. map中 swap的用法:Map中的swap不是一個容器中的元素交換,而是兩個容器交換;For example:#include<map>#include<iostream>

usingnamespace std;

int main(){map <int, int> m1, m2, m3;map <int,int>::iterator m1_Iter;

m1.insert( pair <int, int>(1, 10 ) );m1.insert ( pair <int,int> ( 2, 20 ) );m1.insert ( pair <int,int> ( 3, 30 ) );m2.insert ( pair <int,int> ( 10, 100 ) );m2.insert ( pair <int,int> ( 20, 200 ) );m3.insert ( pair <int,int> ( 30, 300 ) );

cout << "The original map m1is:";for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )cout << " "<<m1_Iter->second;cout << "."<< endl;

// This isthe member function version of swap// m2 is said to be theargument map; m1 the target mapm1.swap( m2);

cout << "Afterswapping with m2, map m1 is:";for ( m1_Iter = m1.begin( ) ; m1_Iter != m1.end() ; m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;

cout << "After swapping with m2, mapm2 is:";for ( m1_Iter = m2.begin( ); m1_Iter != m2.end(); m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;

// This is the specialized template version of swapswap( m1, m3 );

cout << "Afterswapping with m3, map m1 is:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end(); m1_Iter++ )cout << " "<< m1_Iter ->second;cout << "."<< endl;}

6. map的sort問題:Map中的元素是自動按key升序排序,所以不能對map用sort函數(shù):For example:#include<map>#include<iostream>

usingnamespace std;

int main( ){map<int, int> m1;map <int,int>::iterator m1_Iter;

m1.insert (pair <int, int> (1, 20 ) );m1.insert ( pair<int, int> ( 4, 40) );m1.insert ( pair<int, int> ( 3, 60) );m1.insert ( pair<int, int> ( 2, 50) );m1.insert ( pair<int, int> ( 6, 40) );m1.insert ( pair<int, int> ( 7, 30) );

cout<< "The original map m1is:"<<endl;for ( m1_Iter = m1.begin( );m1_Iter != m1.end( ); m1_Iter++ )cout << m1_Iter->first<<""<<m1_Iter->second<<endl;}

The original map m1 is:1 202 503 604 406 407 30

7. map的基本操作函數(shù):C++Maps 是一種關(guān)聯(lián)式容器,包含“關(guān)鍵字/值”對begin() 返回指向map頭部的迭代器clear() 刪除所有元素count() 返回指定元素出現(xiàn)的次數(shù)empty() 如果map為空則返回trueend() 返回指向map末尾的迭代器equal_range() 返回特殊條目的迭代器對erase() 刪除一個元素find() 查找一個元素get_allocator() 返回map的配置器insert() 插入元素key_comp() 返回比較元素key的函數(shù)lower_bound(k) 返回鍵值一個迭代器,該迭代器指向第一個鍵值不小于k的元素。

upper_bound(k) 最后一個大于等于k的位置,也是有一個新元素k進(jìn)來時的插入位置

max_size() 返回可以容納的最大元素個數(shù)rbegin() 返回一個指向map尾部的逆向迭代器rend() 返回一個指向map頭部的逆向迭代器size() 返回map中元素的個數(shù)swap() 交換兩個mapvalue_comp() 返回比較元素value的函數(shù)

multimap[cpp] view plain copy print?//multimap允許重復(fù)的鍵值插入容器  //        **********************************************************        //        * pair只包含一對數(shù)值:pair<int,char>                       *  //        * map是一個集合類型,永遠(yuǎn)保持排好序的,                   *  //  pair  * map每一個成員就是一個pair,例如:map<int,char>           *  //        * map的insert()可以把一個pair對象作為map的參數(shù),例如map<p> *  //        ***********************************************************  #pragma warning(disable:4786)  #include<map>  #include<iostream>  using namespace std;    int main(void)  {   multimap<int,char*> m;   //multimap的插入只能用insert()不能用數(shù)組   m.insert(pair<int,char*>(1,"apple"));      m.insert(pair<int,char*>(1,"pear"));//apple和pear的價錢完全有可能是一樣的   m.insert(pair<int,char*>(2,"banana"));   //multimap的遍歷只能用迭代器方式不能用數(shù)組   cout<<"***************************************"<<endl;   multimap<int,char*>::iterator i,iend;   iend=m.end();   for(i=m.begin();i!=iend;i++)   {    cout<<(*i).second<<"的價錢是"     <<(*i).first<<"元/斤/n";   }   cout<<"***************************************"<<endl;      //元素的反相遍歷   multimap<int,char*>::reverse_iterator j,jend;   jend=m.rend();   for(j=m.rbegin();j!=jend;j++)   {    cout<<(*j).second<<"的價錢是"     <<(*j).first<<"元/斤/n";   }   cout<<"***************************************"<<endl;   //元素的搜索find(),pair<iterator,iterator>equal_range(const key_type &k)const      //和multiset的用法一樣   multimap<int,char*>::iterator s;   s=m.find(1);//find()只要找到一個就行了,然后立即返回。   cout<<(*s).second<<"    "    <<(*s).first<<endl;   cout<<"鍵值等于1的元素個數(shù)是:"<<m.count(1)<<endl;   cout<<"***************************************"<<endl;   //刪除 erase(),clear()   m.erase(1);      for(i=m.begin();i!=iend;i++)   {    cout<<(*i).second<<"的價錢是"     <<(*i).first<<"元/斤/n";   }      return 0;  }  
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 屏边| 九龙坡区| 扶沟县| 宣汉县| 隆尧县| 乌海市| 济阳县| 新和县| 德令哈市| 科技| 阿巴嘎旗| 溧水县| 新兴县| 遂溪县| 五华县| 荥经县| 杂多县| 昆山市| 故城县| 堆龙德庆县| 石台县| 甘谷县| 河西区| 新宁县| 陇川县| 会宁县| 多伦县| 安义县| 镶黄旗| 耒阳市| 大邑县| 孝感市| 惠州市| 青龙| 中江县| 镇雄县| 松原市| 绥阳县| 德昌县| 阳西县| 鹤岗市|