C++ multimap 的使用
生活随笔
收集整理的這篇文章主要介紹了
C++ multimap 的使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
multimap 說(shuō)明
C++ 中multimap 的主要特點(diǎn)是允許有重復(fù)的key 其他的特點(diǎn)和map 類似
multimap 同樣也是STL中的模板使用的時(shí)候 需要先引入 #include?<set>
#include <iostream>
#include <string>
using namespace std;
#include <map>int main()
{// 創(chuàng)建一個(gè)空的mapmultimap<int, string> a;// 插入值使用value_typea.insert(map<int, string>::value_type(1, "張三"));a.insert(map<int, string>::value_type(2, "李四"));a.insert(map<int, string>::value_type(3, "王二"));// 插入值使用paira.insert(pair<int, string>(4, "趙括"));a.insert(map<int, string>::value_type(3, "李白")); // 允許key重復(fù),可以插入cout <<"multimap的size:"<< a.size() << endl; // 允許key重復(fù),打印結(jié)果為 5// 遍歷map map<int, string>::iterator it;for (it = a.begin(); it != a.end(); it++){cout << (*it).first << endl;cout << (*it).second << endl;}
}
打印結(jié)果
multimap?方法說(shuō)明
| 函數(shù) | 說(shuō)明 |
| begin | 返回指向map頭部的迭代器 |
| end | 返回指向map末尾的迭代器 |
| empty | 判斷map是否為空,為空返回true |
| size | 返回map中元素的個(gè)數(shù) |
| operator[] | 訪問(wèn)元素 |
| insert | 插入元素 |
| clear | 清空map |
| swap | 交換兩個(gè)map |
| find | 查找一個(gè)元素 |
| earse | 刪除一個(gè)元素 |
| rbegin | 返回反向迭代器以反向開(kāi)始 |
| rend | 將反向迭代器返回到反向結(jié)束 |
| cbegin | 將常量迭代器返回到開(kāi)頭 |
| cend | 返回常量迭代器結(jié)束 |
| crbegin | 返回const_reverse_迭代器以反轉(zhuǎn)開(kāi)始 |
| equal_range | 返回特殊條目的迭代器對(duì) |
| lower_bound | ?將迭代器返回到下限 |
| ??upper_bound ????????? | 將迭代器返回到上限 |
| value_comp() | ?返回比較元素value的函數(shù) |
?注意:沒(méi)有at 方法了
demo 練習(xí)
#include <iostream>
#include <string>
using namespace std;
#include <map>int main()
{// 聲明一個(gè)setmultimap<int, string> imap;// 獲取默認(rèn)set的sizecout << imap.size() << endl;// 插入值使用value_typeimap.insert(map<int, string>::value_type(1, "張三"));imap.insert(map<int, string>::value_type(2, "李四"));imap.insert(map<int, string>::value_type(3, "王二"));// 插入值使用pairimap.insert(pair<int, string>(4, "趙括"));// 獲取map sizecout << imap.size() << endl;// 遍歷mapmap<int, string>::iterator it;for (it = imap.begin(); it != imap.end(); it++){cout << (*it).first << endl;cout << (*it).second << endl;}// map 判空if (imap.empty()){cout << "map為空" << endl;}else{cout << "map不為空" << endl;}// 清空mapimap.clear();return 0;
}
總結(jié)
以上是生活随笔為你收集整理的C++ multimap 的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++ map 的使用
- 下一篇: HarmonyOS ScrollView