关于map

来源:互联网 发布:网络打字员工作 编辑:程序博客网 时间:2024/06/05 12:40

最基本的操作:插入(四种方法)、遍历(顺序和逆序)

#include <iostream>#include <map>#include <string>using namespace std;map<int, string> mp;/*   插入的前三种方法的返回值是 pair<iterator, bool>*/void display(){    //插入的四种方法    //方法1    mp.insert(pair<int, string> (1, "string01"));    mp.insert(pair<int, string> (2, "string02"));    //方法2    mp.insert(make_pair(3, "string03"));    mp.insert(make_pair(4, "string04"));    //方法3    mp.insert(map<int, string>::value_type(5, "string05"));    mp.insert(map<int, string>::value_type(6, "string06"));    //方法4    mp[7] = "string07";    mp[8] = "string08";    //容器的遍历    //正向遍历    for(map<int, string>::iterator it = mp.begin(); it != mp.end(); it++)    {        cout << it->first << " " << it->second << endl;    }    cout << endl;    //反向遍历    for(map<int, string>::reverse_iterator rit = mp.rbegin(); rit != mp.rend(); rit++)    {        cout << rit->first << " " << rit->second << endl;    }}int main(){    display();    return 0;}

map的insert方法返回值,返回 pair<iterator, bool>   : 如果key已经存在,则会报错

                        数组方法:如果key已经存在,则会覆盖

#include <iostream>#include <map>#include <string>using namespace std;map<int, string> mp;/*   插入的前三种方法的返回值是 pair<iterator, bool>*/void judge(pair<map<int, string>::iterator, bool> _pair){    if(_pair.second == true)    {        cout << "插入成功" << endl;    }    else{        cout << "插入失败" << endl;    }}void display(){    //插入的四种方法    //方法1    pair<map<int, string>::iterator, bool> pair1 = mp.insert(pair<int, string> (1, "string01"));    pair<map<int, string>::iterator, bool> pair2 = mp.insert(pair<int, string> (2, "string02"));    //方法2    pair<map<int, string>::iterator, bool> pair3 = mp.insert(make_pair(3, "string03"));    pair<map<int, string>::iterator, bool> pair4 = mp.insert(make_pair(4, "string04"));    //方法3    pair<map<int, string>::iterator, bool> pair5 = mp.insert(map<int, string>::value_type(5, "string05"));    judge(pair5);    pair<map<int, string>::iterator, bool> pair6= mp.insert(map<int, string>::value_type(5, "string06"));    judge(pair6);    //方法4    mp[7] = "string07";    mp[7] = "string08";//把前面的覆盖    //容器的遍历    //正向遍历    for(map<int, string>::iterator it = mp.begin(); it != mp.end(); it++)    {        cout << it->first << " " << it->second << endl;    }    cout << endl;    //反向遍历    for(map<int, string>::reverse_iterator rit = mp.rbegin(); rit != mp.rend(); rit++)    {        cout << rit->first << " " << rit->second << endl;    }}int main(){    display();    return 0;}

map的查找

#include <iostream>#include <map>#include <string>using namespace std;map<int, string> mp;void display(){    //插入的四种方法    //方法1    mp.insert(pair<int, string> (1, "string01"));    mp.insert(pair<int, string> (2, "string02"));    //方法2    mp.insert(make_pair(3, "string03"));    mp.insert(make_pair(4, "string04"));    //方法3    mp.insert(map<int, string>::value_type(5, "string05"));    mp.insert(map<int, string>::value_type(6, "string06"));    //方法4    mp[7] = "string07";    mp[8] = "string08";    //容器的遍历    for(map<int, string>::iterator it = mp.begin(); it != mp.end(); it++)    {        cout << it->first << " " << it->second << endl;    }    cout << endl;    //map的查找    map<int, string>::iterator it = mp.find(100);    if(it == mp.end())    {        cout << "key 100的值 不存在" << endl;    }    else    {        cout << (*it).first << " " << (*it).second << endl;    }    map<int, string>::iterator it2 = mp.find(5);    if(it2 == mp.end())    {        cout << "key 5的值 不存在" << endl;    }    else    {        cout << (*it2).first << " " << (*it2).second << endl;    }}int main(){    display();    return 0;}
map的查找   之  equal_range

#include <iostream>#include <map>#include <string>using namespace std;map<int, string> mp;void display(){    //插入的四种方法    //方法1    mp.insert(pair<int, string> (1, "string01"));    mp.insert(pair<int, string> (2, "string02"));    //方法2    mp.insert(make_pair(3, "string03"));    mp.insert(make_pair(4, "string04"));    //方法3    mp.insert(map<int, string>::value_type(5, "string05"));    mp.insert(map<int, string>::value_type(6, "string06"));    //方法4    mp[7] = "string07";    mp[8] = "string08";    //容器的遍历    for(map<int, string>::iterator it = mp.begin(); it != mp.end(); it++)    {        cout << it->first << " " << it->second << endl;    }    cout << endl;    //equal_range   返回两个迭代器,形成 pair    pair<map<int, string>::iterator, map<int, string>::iterator> mypair = mp.equal_range(5);    //第一个迭代器  >= 5 的位置    //第二个迭代器  = 5 的位置    if(mypair.first == mp.end()){        cout << "第一个迭代器 >= 5 的位置不存在" << endl;    }    else{        cout << mypair.first->first << " " << mypair.first->second << endl;    }    if(mypair.second == mp.end()){        cout << "第二个迭代器 > 5 的位置不存在" << endl;    }    else{        cout << mypair.second->first << " " << mypair.second->second << endl;    }}int main(){    display();    return 0;}

Multimap 案例
一个key值可以对应多种value -> 分组
假如公司有销售部 sale(员工2名),那么,一个销售部对应一个key 两名员工对应 两个value
人员信息有:姓名,年龄,电话,工资等组成
通过 multimap 进行信息的插入,保存 和显示
分部门显示员工信息

#include <iostream>#include <map>#include <string>using namespace std;struct Person{    string name;    int age;    string tel;//电话    double salary;};multimap<string, Person> mp;int main(){    Person a, b, c, d;    a.name = "aaa";    a.age = 3;    b.name = "bbb";    b.age = 4;    c.name = "ccc";    c.age = 5;    d.name = "ddd";    d.age = 6;    multimap<string, Person>::iterator it;        //插入操作    mp.insert(make_pair("销售部", a));    mp.insert(make_pair("销售部", b));    mp.insert(make_pair("销售部", c));    mp.insert(make_pair("研发部", d));        //遍历操作    for(it = mp.begin(); it != mp.end(); it++)    {        cout << it->first << ": " << it->second.name << " " << it->second.age << endl;    }    //输出部门人数    cout << "销售部的人数: " << mp.count("销售部") << endl;    //find操作    multimap<string, Person>::iterator it2 = mp.find("销售部");    int tag = 0;    while(it2 != mp.end() && tag < mp.count("销售部")){        cout << it2->first << " " << it2->second.name << " " << it2->second.age << endl;        it2++;        tag++;    }    return 0;}

                                             
0 0
原创粉丝点击