Can you use map and set???

一、map和set的引入

        我们都知道,STL在C++ 中得到了广泛使用,它不仅仅由于是提供了类似vector,list等方便使用的容器,更是因为它封装了许多复杂的数据结构算法和大量数据结构的操作。vector封装数组,list封装链表,而map和set则是用来封装二叉树的。

        还有要知道的就说STL中的容器分为两大类:序列式容器和关联式容器,其中vector和list属于序列式容器,而map和set则属于关联式容器,那么为什么map和set的插入删除效率比其他序列式容器高呢?原因就是关联式容器不需要进行内存拷贝和内存移动,所有元素都是以节点的方式进行存储的,插入删除自然更高效。

二、map在库中的基础知识
1、map的模板参数:

2、常用接口:










3、map运用的例子:统计水果出现的次数以及前K种出现次数最多的水果

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
#include <iostream>  
#include <set>  
#include <map>  
#include <algorithm>  
#include <string>  
#include <vector>  
using namespace std;  
  
template <typename K,typename V>  
void sortMap(string fruits[],size_t sz,const map<K,V> &m)//统计出现最多的前k种水果(降序)  
{  
    map <string,int> count;  
      
    for (size_t i = 0; i < sz; ++i)  
    {  
        count[fruits[i]]++;  
    }  
    //将迭代器导入vector  
    vector<map<string,int>::iterator> heap;  
    map<string,int>::iterator mapIt = count.begin();  
    while(mapIt != count.end())  
    {  
        heap.push_back(mapIt);  
        ++mapIt;  
    }  
    //建小堆  
    struct Com  
    {  
        bool operator()(const map<string,int>::iterator l,const map<string,int>::iterator r)  
        {  
            return l->second < r->second;  
        }  
    };  
    sort(heap.begin(),heap.end(),Com());  
  
    //make_heap(heap.begin(),heap.end(),Com());  
    //int diff = heap.size() - k;  
    向下调整  
    //while(diff--)  
    //{  
    //  pop_heap(heap.begin(),heap.end(),Com());  
    //  heap.pop_back();  
    //}  
    //输出结果  
    while(heap.size() != 0)  
    {  
        cout<<heap.back()->first<<":"<<heap.back()->second<<endl;  
        heap.pop_back();  
    }  
}  
  
void CountMap(string fruits[],size_t sz)//统计水果出现的次数  
{  
    map <string,int> count;  
  
    for (size_t i = 0; i < sz; ++i)  
    {  
    方法1  
    //  map <string,int>::iterator it = count.find(fruits[i]);  
    //  if (it != count.end())  
    //  {  
    //      it->second++;  
    //      //*(it).second++;  
    //  }  
    //  else  
    //  {  
    //      count.insert(pair<string,int>(fruits[i],1));  
    //  }  
  
        方法2  
        //pair <map<string,int>::iterator,bool> ret;  
        //ret = count.insert(pair<string,int>(fruits[i],1));  
        //if (ret.second == false)  
        //{  
        //  ret.first->second++;  
        //}  
  
        //方法3  
        count[fruits[i]]++;  
    }  
  
}  
  
  
int main()  
{  
    map <string,int> count;  
    string fruits[] = {"apple","banana","orange","pineapple","grape",  
        "banana","orange","apple","apple","orange",  
        "pineapple","grape","orange","pineapple","pear",  
        "grapefruit","mango","mango juice","apple",  
        "Hami melon","melon","peach","banana",  
        "pineapple","grape","orange","pineapple","pear",  
        "grapefruit","mango","apple","melon","peach","banana",  
        "melon","peach","banana", "Hami melon",  
        "pineapple","grape","orange","pineapple","pear",  
        "grapefruit","mango","peach","peach"};  
    size_t sz = sizeof(fruits)/sizeof(fruits[0]);  
    CountMap(fruits,sz);  
    sortMap(fruits,sz,count);  
    system("pause");  
    return 0;  
}  

统计水果出现次数的过程截图:




运行结果:





三、set在库中的基本内容

1、set的模板参数





2、常用接口









3、set运用实例

set的插入和删除:







代码运用:


[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
void PrintSet(const set<int>& s)  
{  
    set <int>::iterator it = s.begin();  
    while (it != s.end())  
    {  
        cout<<*it<<" ";  
        it++;  
    }  
    cout<<endl;  
}  
  
void TestSet()  
{  
    set <int> s;  
  
    //1普通插入,返回pair类型  
    pair<set<int>::iterator,bool> ret;  
  
    ret = s.insert(2);  
    if (ret.second == true)  
    {  
        cout<<"插入成功!"<<endl;  
    }  
    else  
    {  
        cout<<"插入的值已存在,插入失败!"<<endl;  
    }  
  
    ret = s.insert(4);  
    if (ret.second == true)  
    {  
        cout<<"插入成功!"<<endl;  
    }  
    else  
    {  
        cout<<"插入的值已存在,插入失败!"<<endl;  
    }  
  
    ret = s.insert(2);  
    if (ret.second == true)  
    {  
        cout<<"插入成功!"<<endl;  
    }  
    else  
    {  
        cout<<"插入的值已存在,插入失败!"<<endl;  
    }  
    PrintSet(s);  
      
    //2、根据迭代器位置进行插入,返回一个迭代器  
    set<int>::iterator it = s.find(2);  
    set<int>::iterator retIt = s.insert(it,3);  
  
    if (retIt == s.end())  
    {  
        cout<<"插入的值已存在,插入失败"<<endl;  
    }  
    else  
    {  
        cout<<"插入成功!"<<endl;  
    }  
  
    retIt = s.insert(it,4);  
  
    if (retIt == s.end())  
    {  
        cout<<"插入的值已存在,插入失败"<<endl;  
    }  
    else  
    {  
        cout<<"插入成功!"<<endl;  
    }  
    PrintSet(s);  
      
    //3、插入一段迭代器区间,无返回值  
    vector<int> v;  
    v.push_back(5);  
    v.push_back(6);  
    v.push_back(7);  
  
    vector<int>::iterator start = v.begin();  
    vector<int>::iterator tail = v.end();  
  
    s.insert(start,tail);  
    PrintSet(s);  
  
    //set的删除  
    s.insert(0);  
    s.insert(1);  
    s.insert(2);  
    s.insert(3);  
    s.insert(4);  
    s.insert(5);  
    PrintSet(s);  
  
    //1.通过迭代器位置删除,无返回值  
    set<int>::iterator eraseIt = s.find(0);  
    s.erase(eraseIt);  
    PrintSet(s);  
  
    //2.通过值删除,删除成功返回1,不存在返回0  
    size_t a = s.erase(0);  
    PrintSet(s);  
  
    //3.删除迭代器区间,无返回值  
    s.erase(++s.begin(),--s.end());//只留下第一个和最后一个,其余全部删除  
    PrintSet(s);  
}  
  
int main()  
{  
      
    TestSet();  
    system("pause");  
    return 0;  
}  

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值