C++输出map容器中所有相同value元素的若干方法

初学C++,遇到这一问题。网上Java版本的一堆,C++版本的很少见,特此分享出来。
欢迎大神补充。

#include<iostream>
using namespace std;
#include"map"
#include"string"
class Person
{
public:
        string name;
        int age;
        string tel;
        double salary;
};
void test01()
{
        Person p1, p2, p3, p4, p5;
        p1.name = "王1";
        p1.age = 31;

        p2.name = "王2";
        p2.age = 32;
        p3.name = "张3";
        p3.age = 33;
        p4.name = "张4";
        p4.age = 34;  
        p5.name = "赵5";

        multimap<string, Person>map2;
        map2.insert(make_pair("sale", p1));
        map2.insert(make_pair("sale", p2));

        map2.insert(make_pair("development", p3));
        map2.insert(make_pair("development", p4));

        map2.insert(make_pair("Financial", p5));

        for (multimap<string, Person>::iterator it = map2.begin(); it != map2.end(); it++)
        {
                cout << it->first << "\t" << it->second.name << endl;
        }
        cout << "遍历结束" << endl;
       
        cout << "development部分人数:  " << map2.count("development") << endl;
        cout << "development部门员工信息" << endl;
        multimap<string, Person>::iterator it2 = map2.find("development");
        //multimap<string, Person>::iterator it2;
        //第一种方法输出某一部们的所有人
        //int num2 = map2.count("development");
        //int tag = 0;
        //while (it2 != map2.end()&&tag<num2)
        //{
        //        //it2 = map2.find("development");
        //        string tmp = it2->first;
        //        cout << it2->first << "\t" << it2->second.name << endl;
        //        it2++;
        //        tag++;
        //}
        //第二种方法输出某一部们的所有人
        string tmp = it2->first;
        while (it2 != map2.end())
        {
                cout << it2->first << "\t" << it2->second.name << endl;
                it2++;
                if (it2->first!= tmp)//如果不是同一部门就中断退出
                {
                        break;
                }
        }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞天的大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值