#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
///1. 初始化
multimap<int, string> mapStudent;
multimap<int, string>::iterator iter, beg, end;
///2. 添加元素
///multimap不支持下标操作
mapStudent.insert(pair<int, string>(0, "student_one"));
mapStudent.insert(pair<int, string>(0, "student_one_copy"));///一对多
mapStudent.insert(pair<int, string>(1, "student_two"));
mapStudent.insert(pair<int, string>(5, "Fear Kubrick"));
mapStudent.insert(pair<int, string>(2, "Akemi Homura"));
mapStudent.insert(pair<int, string>(-1, "Eren Jaeger"));
mapStudent.insert(pair<int, string>(99, "lin"));
cout << mapStudent.size() << endl;
cout << endl;
///3. 遍历
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout << iter->first << " " << iter->second << endl;
cout << endl;
///4. 单键查询与范围查询
///单键查询
int count = mapStudent.count(0);
iter = mapStudent.find(0);
for (int i = 0; i < count; i++, iter++)
cout << iter->first << " " << iter->second << endl;
cout << endl;
///范围查询
beg = mapStudent.lower_bound(1);/// >=1
end = mapStudent.upper_bound(5);/// <=5
for (; beg != end; beg++)
cout << beg->first << " " << beg->second << endl;
cout << endl;
///5. 删除
iter = mapStudent.find(1);
mapStudent.erase(iter);
cout << mapStudent.size() << endl;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout << iter->first << " " << iter->second << endl;
cout << endl;
///6. 判空与清空
if (!mapStudent.empty())
mapStudent.clear();
}
C++ STL 练手(multimap的使用)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...
- vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...
- vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...
- vector的使用 list的使用 deque的使用 set的使用 map的使用 multiset的使用 mult...