zoukankan      html  css  js  c++  java
  • C++(十一)— map的插入、查找、删除

      标准库map类型是一种以键-值(key-value)存储的数据类型。

      map是STL的一个关联容器。它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。

    1、map特点

      map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key

      自动建立Key - value的对应。key 和 value可以是任意你需要的类型。

      根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。

      快速插入Key -Value 记录。快速删除记录,根据Key 修改value记录。遍历所有记录。

    2、map对象的一些基本操作

      因此,若只是查找该元素是否存在,可以使用函数count(k),该函数返回的是k出现的次数;若是想取得key对应的值,可以使用函数find(k),该函数返回的是指向该元素的迭代器。

      第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

      第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。

      查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.

        map<char, int> p;
        //map中插入元素
        // method_1
        p.insert(make_pair('a', 10));
        p.insert(make_pair('b', 9));
        // method_2
        p.insert(pair<char, int>('c', 8));
        p.insert(pair<char, int>('d', 7));
        //method_3
        p.insert(map<char, int>::value_type('e', 6));
    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include <vector>
    #include<string>
    #include<map>
    using namespace std;
    
    int main()
    {
        map<char, int> p;
        //map中插入元素
        p.insert(make_pair('a',10));
        p.insert(make_pair('c',9 ));
        p.insert(make_pair('b',10 ));
        //采用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。
        //p['y'] = 17;
        //p['f'] = 11;
    
        map<char, int>::iterator it;
        for (it = p.begin(); it != p.end(); it++)
            cout << (*it).first << "   " << (*it).second << endl;
    
        /*用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,
        由于map的特性,一对一的映射关系,
        就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了*/
        if (p.count('b'))
        {
            //返回出现次数,0或者1.
            cout << p.count('b') << endl;
        }
    
        //用find函数来定位数据出现位置,它返回的一个迭代器
        map<char, int>::iterator iter;
        iter = p.find('b');
        if (iter != p.end())
            cout << "Find, the value is " << (*iter).second << endl;
        else
            cout << "Do not Find" << endl;
    
        system("pause");
        return 0;
    }

      遍历map中的内容:使用迭代器的方法、使用指针的方法

    map<string, int>::iterator iter;
        for (iter = name_score_map.begin(); iter != name_score_map.end(); ++iter) {
            cout << (*iter).first << endl;
        }
    for (const auto& iter : name_score_map)
            cout << iter.first << "	" << iter.second << endl;

      删除元素方法。

    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include <vector>
    #include<string>
    #include<map>
    using namespace std;
    
    int main()
    {
        map<char, int> p;
        //map中插入元素
        p.insert(make_pair('a',10));
        p.insert(make_pair('c',9 ));
        p.insert(make_pair('b',10 ));
        //采用下标的方法读取map中元素时,若map中不存在该元素,则会在map中插入。
        //p['y'] = 17;
        //p['f'] = 11;
    
        map<char, int>::iterator it;
        for (it = p.begin(); it != p.end(); it++)
            cout << (*it).first << "   " << (*it).second << endl;
    
        //如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好  
        //如果要删除1,用迭代器删除  
        map<char, int>::iterator iter;
        iter = p.find(1);
        p.erase(iter);
        //如果要删除1,用关键字删除  
        int n = p.erase(1);//如果删除了会返回1,否则返回0  
        //用迭代器,成片的删除  一下代码把整个map清空  
        p.erase(p.begin(), p.end());
        //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合  
        //自个加上遍历代码,打印输出吧  
    
        system("pause");
        return 0;
    }
    equal_range的用法
       map<char, int> p;
        //map中插入元素
        // method_1
        p.insert(make_pair('a', 10));
        p.insert(make_pair('a', 9));
        // method_2
        p.insert(pair<char, int>('a', 8));
        p.insert(pair<char, int>('a', 7));
        //method_3
        p.insert(map<char, int>::value_type('a', 6));
    
        //equal_range
        pair<map<char, int>::iterator, map<char, int>::iterator> mypair = p.equal_range('a');
        if (mypair.first != p.end())
        {
            cout << mypair.first->first << "	" << mypair.first->second << endl;
        }
        else
        {
            cout << "第一个迭代器 >=c 的位置不存在" << endl;
        }
    
        if (mypair.second != p.end())
        {
            cout << mypair.second->first << "	" << mypair.second->second << endl;
        }
        else
        {
            cout << "第二个迭代器 >c 的位置不存在" << endl;
        }

       输出结果:(由于不存在大于 a的迭代器,所以第二个找不到)

  • 相关阅读:
    张礼鑫(帮别人名字作诗)
    千年的守望
    感恩
    转载:像成功人士一样释放压力,快乐并高效着
    50个MBA精粹 [推荐]
    赚钱不是人生的全部
    世界上最权威的68句创业名言,哪一句让你获益匪浅
    郝雪涛只爱薛乙静(帮恋人名字作诗)
    心爱的,你为何还不来为我们的爱情赴约
    中国,请还我一个诗的天堂
  • 原文地址:https://www.cnblogs.com/eilearn/p/9466905.html
Copyright © 2011-2022 走看看