c++ STL "multimap"

#include <iostream>
using namespace std;
#include "map"
#include "string"

class Person
{
public:
    string name;
    int age;
    string tel;
    double saly;
};


//multimap的创建
void main1()
{
    Person p1, p2, p3, p4, p5;
    p1.name = "王1";
    p1.age = 31;

    p2.name = "王2";
    p2.age = 32;

    p3.name = "张3";
    p3.age = 22;

    p4.name = "张4";
    p4.age = 23;

    p5.name = "赵5";
    p5.age = 25;
    multimap<string, Person> map2;
    map2.insert(make_pair("sale", p1));
    map2.insert(make_pair("sale", p2));
    map2.insert(make_pair("dev", p3));
    map2.insert(make_pair("dev", p4));
    map2.insert(make_pair("dev", p5));

    for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++)
    {
        cout << it->first << "\t" << it->second.name << endl;
    }
    cout << "遍历结束" << endl;

    //部分遍历
    int dev_count = map2.count("dev");
    cout << "开发部人数==>" << dev_count << endl;
    multimap<string, Person>::iterator it1 = map2.find("dev");
    int tag = 0;
    while (it1!=map2.end() &&tag<dev_count)
    {
        cout << it1->first << "\t" << it1->second.name << endl;
        it1++;
        tag++;
    }
    //修改元素
    cout << "修改元素" << endl;
    for (multimap<string, Person>::iterator it2 = map2.begin(); it2 != map2.end(); it2++)
    {
        if (it2->second.age == 22)
        {
            it2->second.name = "张伟伟";
        }
    }
    for (multimap<string, Person>::iterator it3 = map2.begin(); it3 != map2.end(); it3++)
    {
        cout << it3->first << "\t" << it3->second.name << endl;

    }

}

void main()
{
    main1();
    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值