一:介紹
map是STL的關聯式容器,以key-value的形式存儲,以紅黑樹(平衡二叉查找樹)作為底層數據結構,對數據有自動排序的功能。
命名空間為std,所屬頭文件<map> 注意:不是<map.h>
二:常用操作
容量:
修改:
迭代器:
三:存儲
map<int, string> map1; //方法1: map1.insert(pair<int, string>(2, "beijing")); //方法2: map1[4] = "changping"; //方法3: map1.insert(map<int, string>::value_type(1, "huilongguan")); //方法4: map1.insert(make_pair<int, string>(3, "xierqi"));
四:遍歷
for (map<int, string>::iterator it=map1.begin(); it!=map1.end(); it++) { cout << it->first << ":" << it->second << endl; }
五:查找
string value1 = map1[2]; if (value1.empty()) { cout << "not found" << endl; } //方法2 map<int, string>::iterator it = map1.find(2); if (it == map1.end()) { cout << "not found" << endl; } else { cout << it->first << ":" << it->second << endl; }
六:修改
//修改數據 map1[2] = "tianjin";
七:刪除
//方法1 map1.erase(1); //方法2 map<int, string>::iterator it1 = map1.find(2); map1.erase(it1);
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答