关于map

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

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值