maplive[112]="APRil";//map中最簡單最常用的插入添加!4. map中元素的查找:
find()函數返回一個迭代器指向鍵值為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函數: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的基本操作函數:C++Maps 是一種關聯式容器,包含“關鍵字/值”對begin() 返回指向map頭部的迭代器clear() 刪除所有元素count() 返回指定元素出現的次數empty() 如果map為空則返回trueend() 返回指向map末尾的迭代器equal_range() 返回特殊條目的迭代器對erase() 刪除一個元素find() 查找一個元素get_allocator() 返回map的配置器insert() 插入元素key_comp() 返回比較元素key的函數lower_bound(k) 返回鍵值一個迭代器,該迭代器指向第一個鍵值不小于k的元素。
upper_bound(k) 最后一個大于等于k的位置,也是有一個新元素k進來時的插入位置
max_size() 返回可以容納的最大元素個數rbegin() 返回一個指向map尾部的逆向迭代器rend() 返回一個指向map頭部的逆向迭代器size() 返回map中元素的個數swap() 交換兩個mapvalue_comp() 返回比較元素value的函數
multimap[cpp] view plain copy print?//multimap允許重復的鍵值插入容器 // ********************************************************** // * pair只包含一對數值:pair<int,char> * // * map是一個集合類型,永遠保持排好序的, * // pair * map每一個成員就是一個pair,例如:map<int,char> * // * map的insert()可以把一個pair對象作為map的參數,例如map<p> * // *********************************************************** #pragma warning(disable:4786) #include<map> #include<iostream> using namespace std; int main(void) { multimap<int,char*> m; //multimap的插入只能用insert()不能用數組 m.insert(pair<int,char*>(1,"apple")); m.insert(pair<int,char*>(1,"pear"));//apple和pear的價錢完全有可能是一樣的 m.insert(pair<int,char*>(2,"banana")); //multimap的遍歷只能用迭代器方式不能用數組 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的元素個數是:"<<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; }