日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

map映射

發(fā)布時間:2025/3/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 map映射 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

map實例代碼:

1 // UVa156 Ananagrams 2 // Rujia Liu 3 // 題意:輸入一些單詞,找出所有滿足如下條件的單詞:該單詞不能通過字母重排得到輸入文本中的另外一個單詞 4 // 算法:把每個單詞“標準化”,即全部轉(zhuǎn)化為小寫字母然后排序,然后放到map中進行統(tǒng)計 5 #include<iostream> 6 #include<string> 7 #include<cctype> 8 #include<vector> 9 #include<map> 10 #include<algorithm> 11 using namespace std; 12 13 map<string,int> cnt; 14 vector<string> words; 15 16 // 將單詞s進行“標準化” 17 string repr(string s) { 18 string ans = s; 19 for(int i = 0; i < ans.length(); i++) 20 ans[i] = tolower(ans[i]); 21 sort(ans.begin(), ans.end()); 22 return ans; 23 } 24 25 int main() { 26 int n = 0; 27 string s; 28 while(cin >> s) { 29 if(s[0] == '#') break; 30 words.push_back(s); 31 string r = repr(s); 32 if(!cnt.count(r)) cnt[r] = 0; 33 cnt[r]++; 34 } 35 vector<string> ans; 36 for(int i = 0; i < words.size(); i++) 37 if(cnt[repr(words[i])] == 1) ans.push_back(words[i]); 38 sort(ans.begin(), ans.end()); 39 for(int i = 0; i < ans.size(); i++) 40 cout << ans[i] << "\n"; 41 return 0; 42 } View Code

?

map添加數(shù)據(jù):

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

map中元素的查找:

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

1 map<int ,string >::iterator l_it;; 2 l_it=maplive.find(112); 3 if(l_it==maplive.end()) 4 cout<<"we do not find 112"<<endl; 5 else cout<<"wo find 112"<<endl;

map中元素的刪除:

1 map<int ,string >::iterator l_it;; 2 l_it=maplive.find(112); 3 if(l_it==maplive.end()) 4 cout<<"we do not find 112"<<endl; 5 else maplive.erase(l_it); //delete 112;

map的sort問題:
??Map中的元素是自動按key升序排序,所以不能對map用sort函數(shù)

1 #include <map> 2 #include <iostream> 3 using namespace std; 4 int main( ) 5 { 6 map <int, int> m1; 7 map <int, int>::iterator m1_Iter; 8 m1.insert ( pair <int, int> ( 1, 20 ) ); 9 m1.insert ( pair <int, int> ( 4, 40 ) ); 10 m1.insert ( pair <int, int> ( 3, 60 ) ); 11 m1.insert ( pair <int, int> ( 2, 50 ) ); 12 m1.insert ( pair <int, int> ( 6, 40 ) ); 13 m1.insert ( pair <int, int> ( 7, 30 ) ); 14 cout << "The original map m1 is:"<<endl; 15 for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) 16 cout << m1_Iter->first<<" "<<m1_Iter->second<<endl; 17 18 } 19 /* 結(jié)果是 20 The original map m1 is: 21 1 20 22 2 50 23 3 60 24 4 40 25 6 40 26 7 30 27 */ 28 請按任意鍵繼續(xù). . .

轉(zhuǎn)載于:https://www.cnblogs.com/cyb123456/p/5798787.html

總結(jié)

以上是生活随笔為你收集整理的map映射的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。