美文网首页程序员
第二章 C++ STL 泛型编程 3

第二章 C++ STL 泛型编程 3

作者: Nautilus1 | 来源:发表于2017-11-30 16:15 被阅读0次

map映照容器

map也是用红黑树实现的,插入元素的键值不允许重复,比较函数针对键值比较。

map函数

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    map<string, float> m;
    m["jack"] = 98;
    m["bomi"] = 96;
    m["kate"] = 97;

    map<string, float>::iterator it;
    for (it = m.begin(); it != m.end(); it ++)
        cout << (*it).first<<":"<<(*it).second<<endl;
    return 0;
}

元素的增删查

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    map<int,char> m;
    //插入元素,按键值的由小到大放入黑白树中
    m[25]='m';
    m[28]='k';
    m[10]='x';
    m[30]='a';

    map<int,char>::iterator it;
    for (it = m.begin(); it != m.end(); it ++)
        cout << (*it).first<<":"<<(*it).second<<endl;

    it = m.find(28);
    if(it!=m.end())//搜索到该键值
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    else
        cout<<"not found it"<<endl;

    //删除键值为 28 的元素
    m.erase(28);
    //反向遍历元素
    map<int,char>::reverse_iterator rit;
    for(rit = m.rbegin(); rit != m.rend(); rit ++)
        cout<<(*rit).first<<" : "<<(*rit).second<<endl;

    return 0;
}

自定义比较函数,与set一样的两种方式:

  1. 如果元素不是结构体,可以编写比较函数。按键值由大到小的顺序将元素插入到 map 中:
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

//自定义比较函数
struct cmp
{
    bool operator()(const int &a,const int &b)
    {
        if(a!=b)
            return a>b;
        else
            return a>b;
    }
};

int main()
{
    map<int, char, cmp> m;
    m[25]='m';
    m[28]='k';
    m[10]='x';
    m[30]='a';

    map<int, char, cmp>::iterator it;
    for(it = m.begin();it != m.end(); it ++)
        cout<<(*it).first<<" : "<<(*it).second<<endl;

    return 0;
}
  1. 如果元素是结构体,,直接把比较函数写在结构体内
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

struct Info
{
    string name;
    float score;
    //重载“<”操作符,自定义排序规则
    bool operator < (const Info &a) const
    {
        //按 score 由大到小排列。如果要由小到大排列,使用“>”号即可
        return a.score < score;
    }

};

int main()
{
    map<Info,int> m;
    Info info;
    //插入元素,按键值的由小到大放入黑白树中
    info.name = "Jack";
    info.score = 60;
    m[info] = 25;
    info.name = "Bomi";
    info.score = 80;
    m[info] = 10;
    info.name = "Peti";
    info.score = 66.5;
    m[info] = 30;

    map<Info,int>::iterator it;
    for(it=m.begin();it!=m.end();it++)
    {
        cout<<(*it).second<<" : ";
        cout<<((*it).first).name<<" "<<((*it).first).score<<endl;
    }

    return 0;
}

用map实现数字分离

把数字当成字符串,使用 map 的映照功能,很方便地实现了数字分离。

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    map<int,char> m;
    //字符映射数字
    for(int j = 0; j < 10; j ++)
        m['0' + j] = j;

    string sa,sb;
    sa="6234";
    int i;
    int sum=0;
    for(i = 0; i <sa.length(); i ++)
    {
        sum += m[sa[i]];
    }
    cout<<"sum = "<<sum<<endl;

    //数字映照字符
    for(int j = 0;j < 10;j ++)
    {
        m[j]='0'+j;
    }
    int n = 7;
    string s ="The number is ";
    cout<<s + m[n]<<endl;

    return 0;
}
=>
sum = 15
The number is 7

multimap多重映照容器

multimap允许插入重复键值元素。

multimap函数

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main()
{
    multimap<string,double> m;
    //插入元素
    m.insert( pair<string,double>("Jack",300.5) );
    m.insert( pair<string,double>("Kity",200) );
    m.insert( pair<string,double>("Memi",500) );
    //重复插入键值“Jack”
    m.insert( pair<string,double>("Jack",306) );
    //使用前向迭代器中序遍历 multimap
    multimap<string,double>::iterator it;
    for(it = m.begin(); it != m.end(); it ++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

    //查找键值
    cout<<endl<<"the searching result:"<<endl;
    it=m.find("Jack");
    if(it!=m.end())//找到
    {
        cout<<(*it).first<<" "<<(*it).second<<endl;
    }
    else//没找到
    {
        cout<<"not find it"<<endl;
    }

    it=m.find("Nacy");
    if(it!=m.end())//找到
    {
        cout<<(*it).first<<" "<<(*it).second<<endl;
    }
    else//没找到
    {
        cout<<"not find it"<<endl;
    }

    cout<<endl;
    //删除键值等于“Jack”的元素
    m.erase("Jack");
    //使用前向迭代器中序遍历 multimap
    cout<<"the elements after deleted:"<<endl;
    for(it=m.begin();it!=m.end();it++)
    {
        cout<<(*it).first<<" : "<<(*it).second<<endl;
    }

    return 0;
}

相关文章

网友评论

    本文标题:第二章 C++ STL 泛型编程 3

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