美文网首页
C++ STL 练手(multimap的使用)

C++ STL 练手(multimap的使用)

作者: 863cda997e42 | 来源:发表于2018-02-12 16:05 被阅读27次
  1. vector的使用
  2. list的使用
  3. deque的使用
  4. set的使用
  5. map的使用
  6. multiset的使用
  7. multimap的使用
#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的使用)

      本文链接:https://www.haomeiwen.com/subject/udzrtftx.html