multimap

!image.png

  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4. void main() {
  5. typedef nultimap<int, double, less<int> > mmid;
  6. mmid pairs;
  7. cout << pairs.count(15) << endl; // 没有输出
  8. pairs.insert(mmid::value_type(15, 2.7));
  9. pairs.insert(mmid::value_type(15, 99.3));
  10. // 输出 2
  11. cout << pairs.count(15) << endl; // 求关键字等于某值的元素个数
  12. pairs.insert(mmid::value_type(30, 111.11));
  13. pairs.insert(mmid::value_type(10, 22.22));
  14. pairs.insert(mmid::value_type(25, 22.22));
  15. pairs.insert(mmid::value_type(20, 22.22));
  16. for(mmid::const_iterator i = pairs.begin(); i != pairs.end(); ++i)
  17. // (10, 22.22),(15,2.7),(15,99.3),(20,22.22),(25,22.22),(30,111.11)
  18. cout << "(" << i->first << "," << i->second << ")" << ",";
  19. }

例题

image.png
image.png

  1. #include <iostream>
  2. #include <cmap>
  3. #include <string>
  4. using namespace std;
  5. class Stu {
  6. public:
  7. typedef struct { // 类的内部还可以定义类
  8. int id;
  9. string name;
  10. }Info;
  11. int score;
  12. Info info;
  13. };
  14. typedef multimap<int, Stu::Info> MAP_STD;
  15. void main() {
  16. MAP_STD mp;
  17. Stu st;
  18. string cmd;
  19. while(cin >> cmd) {
  20. if(cmd == "Add") {
  21. cin >> st.info.name >> st.info.id >> st.score;
  22. mp.insert(MAP_STD::value_type(st.score, st.info));
  23. } else if (cmd == 'Query') {
  24. int score;
  25. cin >> score;
  26. MAP_STD::iterator p = mp.lower_bound(score);
  27. if(p != mp.begin()) {
  28. --p;
  29. score = p->first; // 比要查找的分数低的最高分
  30. MAP_STD::iterator maxp = p;
  31. int max_id = p->second->id;
  32. for(; p != mp.begin() && p->first == score; --p) {
  33. if(p->second.id > max_id) {
  34. maxp = p;
  35. max_id = p->second.id;
  36. }
  37. }
  38. if(p->first == score) { // 如果上面循环因为 p==mp.begin()而终止,还要再操作
  39. if(p->second.id > max_id) {
  40. maxp = p;
  41. max_id = p->second.id;
  42. }
  43. }
  44. cout << maxp->second.name << " " maxp->second.id << " " << maxp->first << endl;
  45. } else { //lower_bound的结果就是 begin,说明没人分数比查询分数低
  46. cout << "Nobody" << endl;
  47. }
  48. }
  49. }
  50. }
  • 注意 lower_bound 的使用, p != mp.begin() 是关键
  • 插入还能这么写
    1. mp.insert(make_pair(st.score, st.info));

    map

    image.png

    map的[]成员函数

    image.png ```cpp

    include

    include

    using nanespace std;

void main() { typedef map > mmid; mmid pairs; cout << pairs.count(15) << endl; // 0 pairs.insert(mmid::value_type(15,2.7)); pairs.insert(make_pair(15, 2.7)); // 插入失败,如何判断见set cout << pairs.count(15) << endl; // 1 pairs.insert(mmid::value_type(20, 9.3)); mmid::iterator i ; for(i = pairs.begin(); i != pairs.end(); ++i) cout << *i << “ “; cout << endl; // (15,2.7) (20,9.3)

  1. int n = pairs[40]; // 如果没有关键字为40的元素,则插入一个
  2. for(i = pairs.begin(); i != pairs.end(); ++i)
  3. cout << *i << ",";
  4. cout << endl; // (15,2.7),(20,9.3),(40,0),
  5. pairs[15] = 6.28; // 把关键字为15的元素值改成6.28
  6. for(i = pairs.begin(); i != pairs.end(); ++i)
  7. cout << *i << ",";
  8. cout << endl; // (15,6.28),(20,9.3),(40,0),

} ```