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

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

C++ STL 學習筆記 map multimap

2019-11-06 07:28:14
字體:
來源:轉載
供稿:網友

map multimap

map,multimap key-value對容器,也叫字典,map中不能存放key相同的元素,而multimap可以,容器中元素默認按升序排序

map multimap的相關操作

m.insert(map<T1,T2>::value_type(a1,a2));m.insert(make_pair(a1,a2));m.insert(pair<T1,T2>(a1,a2));a[a1]=a2; //multimap不能用這種方式m.find(val) //查找元素,返回迭代器m.erase(key) //通過key刪除元素m.erase(it) //通過迭代器刪除元素m.erase(it1,it2) //刪除迭代器范圍內的元素

學習代碼

#include <iostream>#include <map>#include <string>using namespace std;int main(){ map<int, string> a; //鍵不能重復,默認按升序排序 multimap<int, string> ma; a.insert(map<int, string>::value_type(1, "One")); a.insert(map<int, string>::value_type(1, "one")); a.insert(map<int, string>::value_type(2, "Two")); a.insert(map<int, string>::value_type(3, "Three")); a.insert(make_pair(-1, "Minus One")); a.insert(pair<int, string>(1000, "One Thousand")); a[1000000] = "One Million";//multimap不能用這種方式 cout << "最簡單那的查找" << endl; cout << a[3] << endl; cout << "map里一共有" << a.size() << "個kye-value對數據" << endl; map<int, string>::const_iterator it = a.begin(); while (it != a.end()) { cout << "key" << it->first ; cout << " value" << it->second.c_str(); cout << endl; ++it; } ma.insert(multimap<int, string>::value_type(3, "Three")); ma.insert(multimap<int, string>::value_type(45, "Forty Five")); ma.insert(make_pair(-1, "Minus one")); ma.insert(pair<int, string>(1000, "One Thousand")); ma.insert(pair<int, string>(1000, "One Thousand")); multimap<int, string>::const_iterator itm = ma.begin(); while (itm != ma.end()) { cout << "key:" << itm->first; cout << " value:" << itm->second.c_str(); cout << endl; ++itm; } cout << "multimap里有" << ma.count(1000) << "個1000" << endl; multimap<int, string>::const_iterator fi; fi=ma.find(45); if (fi != ma.end()) { cout << "找到了" << fi->first<<" "<<fi->second.c_str()<< endl; } else { cout << "沒找到" << endl; } fi = ma.find(1000); if (fi != ma.end()) { cout << "找到了1000" << endl; size_t n = ma.count(1000); for (size_t i = 0; i < n; i++) { cout << "key:" << fi->first << "value:" << fi->second.c_str() << endl; ++fi; } } else { cout << "沒找到" << endl; } if (ma.erase(-1) > 0) cout << "刪除-1成功" << endl; multimap<int, string>::iterator itm2 = ma.find(45); if (itm2 != ma.end()) { ma.erase(itm2); cout << "刪除45成功" << endl; } ma.erase(ma.lower_bound(1000), ma.upper_bound(1000));//迭代器 getchar(); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 潮州市| 永安市| 广东省| 调兵山市| 寻乌县| 滦平县| 金溪县| 铜陵市| 霸州市| 阳山县| 当雄县| 扎赉特旗| 资兴市| 牡丹江市| 阳东县| 哈密市| 河源市| 静安区| 太保市| 基隆市| 金坛市| 尤溪县| 奇台县| 盈江县| 永修县| 灵石县| 磐石市| 安新县| 靖远县| 南华县| 林西县| 莆田市| 兰州市| 绥滨县| 光山县| 望江县| 巍山| 景泰县| 抚州市| 临西县| 浦江县|