超具体介绍map(multimap)的利用

打印 上一主题 下一主题

主题 870|帖子 870|积分 2610

map类的介绍

        map的声明如下,Key是map底层关键字的类型,T是map底层value的类型。set默认要求Key支持小于比力,如果不支持大概必要的情况下我们可以自行传入仿函数,map底层存储数据的内存是从空间申请来的。一样寻常情况下,我们是不必要传后两个参数的。map的底层是由红黑树实现的,增删改查的效率是logN,迭代器遍历走的是中序,所以Key是有序排列的
        红黑树是平衡二叉搜索树
  1. template < class Key,                                     // map::key_type
  2.            class T,                                       // map::mapped_type
  3.            class Compare = less<Key>,                     // map::key_compare
  4.            class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
复制代码
pair类型介绍

        红黑树的节点类型是pair类型,利用pair<Key,T>存储数据
  1. typedef pair<const Key, T> value_type;
  2. template <class T1, class T2>
  3. struct pair
  4. {
  5. typedef T1 first_type;
  6. typedef T2 second_type;
  7. T1 first;
  8. T2 second;
  9. pair(): first(T1()), second(T2 ())
  10. {}
  11. pair(const T1& a, const T2& b): first(a), second(b)
  12. {}
  13. template<class U, class V>
  14. pair (const pair<U,V>& pr): first(pr.first), second(pr.second)
  15. {}
  16. };
  17. template <class T1,class T2>
  18. inline pair<T1,T2> make_pair (T1 x, T2 y)
  19. {
  20. return ( pair<T1,T2>(x,y) );
  21. }
复制代码
map的增删查

  1. #include<iostream>
  2. #include<map>
  3. using namespace std;
  4. int main()
  5. {
  6.         map<string, string> m;
  7.         //插入有名pair(方式一)
  8.         pair<string, string> s("first", "第一个");
  9.         m.insert(s);
  10.         //插入无名piar(方式二)
  11.         m.insert(pair<string, string>("second", "第二个"));
  12.         //调用make_pair函数
  13.         m.insert(make_pair("third", "第三个"));
  14.         //make_pair函数,其功能为,将两个参数构造成一个pair并返回
  15.         //隐式类型转化(c++11),可以看作将两个元素直接转化为pair
  16.         m.insert({ "auto", "自动的" });
  17.         // initializer_list构造
  18.         map<string, string> dict = { {"left", "左边"}, {"right", "右边"},
  19. {"insert", "插⼊"},{ "string", "字符串" } };
  20.        
  21.         //map的遍历
  22.         //pair并没有重载流插入与流提取,故而下面的输出我们只能通过输出成员变量
  23.         cout << "遍历一:" << endl;
  24.         for (auto& e : m)
  25.         {
  26.                 cout << e.first << "," << e.second << " ";
  27.         }
  28.         cout << endl;
  29.         cout << "遍历二:" << endl;
  30.         auto it = m.begin();
  31.         while (it != m.end())
  32.         {
  33.                 cout << it->first << "," <<it->second<< " ";
  34.                 it++;
  35.         }
  36.         cout << endl;
  37.         //map的查找
  38.         string str;
  39.         cin >> str;
  40.         auto pos = m.find(str);
  41.         if (pos != m.end())
  42.         {
  43.                 cout << "找到了:" << pos->first << "," << pos->second << endl;
  44.         }
  45.         else
  46.         {
  47.                 cout << "不存在!"<<endl;
  48.         }
  49.         //map的删除
  50.         cout << "删除一:" << endl;
  51.         cin >> str;
  52.         m.erase(str);
  53.         for (auto& e : m)
  54.         {
  55.                 cout << e.first << "," << e.second << " ";
  56.         }
  57.         cout << endl;
  58.         cout << "删除二:" << endl;
  59.         cin >> str;
  60.         auto pos0 = m.find(str);
  61.         m.erase(pos0);
  62.         for (auto& e : m)
  63.         {
  64.                 cout << e.first << "," << e.second << " ";
  65.         }
  66.         cout << endl;
  67.         cout << "删除三:" << endl;
  68.         //iterator  erase (const_iterator first, const_iterator last);
  69.         //删除一段区间,给出左边迭代器和右边迭代器,函数将会删除这段区间,
  70.         //一般搭配 lower_bound  upper_bound 使用
  71. }
复制代码
        
map的迭代器和[]功能

·        

        operator[]的功能:传入Key值会返回value值(传入pair的first,会返回pair的second)operator[]具有:插入、查找、修改的功能。必要注意的是,如果当传入的Key值本来就不存在map中时,这个值将会被插入map
        而operator[]之所以拥有这些功能与其底层(insert)的实现有关

        下面我们来仔细看看insert函数 

        insert的返回值为一个pair:
        当map中没有这个元素,insert插入乐成并返回 pair<乐成插入位置的迭代器,true>
        反之当map中有这个元素,insert插入失败并返回  pair<已经存在的值的迭代器,false>
具体实例:
  1. #include<iostream>
  2. #include<map>
  3. using namespace std;
  4. int main()
  5. {
  6.         string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜","苹果", "香蕉", "苹果", "香蕉" };  // 使用正确汉字
  7.         map<string, int> countMap;
  8.         //普通统计水果数量的方式
  9.         for (auto& e : arr)
  10.         {
  11.                 auto pos=countMap.find(e);
  12.                 if (pos != countMap.end())
  13.                 {
  14.                         pos->second++;
  15.                 }
  16.                 else
  17.                 {
  18.                         countMap.insert({ e,1 });
  19.                 }
  20.         }
  21.         for (auto& e : countMap)
  22.         {
  23.                 cout << e.first << "," << e.second<<" ";
  24.         }
  25.         cout << endl;
  26.         //使用operator[]
  27.         for (auto& e : arr)
  28.         {
  29.                 countMap[e]++;
  30.         }
  31.         //若e存在则插入失败,operator[]返回value,再让其+1
  32.         //若e不存在则插入成功{e,0},operator[]返回value,再让其+1 》{e,1}
  33.         for (auto& e : countMap)
  34.         {
  35.                 cout << e.first << "," << e.second << " ";
  36.         }
  37.         cout << endl;
  38.        
  39. }
复制代码
  1. #include<iostream>
  2. #include<map>
  3. using namespace std;
  4. int main()
  5. {
  6.         map<string, string> dict;
  7.         dict.insert(make_pair("sort", "排序"));
  8.         // key不存在-> 插⼊ {"insert", string()}
  9.         dict["insert"];
  10.         // key不存在-> 插⼊+修改
  11.         dict["left"] = "左边";
  12.         // key已经存在-> 修改
  13.         dict["left"] = "左边、剩余";
  14.         // key存在->查找
  15.         cout << dict["left"] << endl;
  16.         return 0;
  17. }
复制代码
multimap和map的差异

        multimap和map的使⽤根本完全类似,重要区别点在于multimap⽀持关键值key冗余,那么 insert/find/count/erase都围绕着⽀持关键值key冗余有所差异,这⾥跟set和multiset完全⼀样,⽐如 find时,有多个key,返回中序第⼀个。其次就是multimap不⽀持[],因为⽀持key冗余,[]就只能⽀ 持插⼊了,不能⽀持修改。 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表