#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");
}
c++ STL "multimap"
最新推荐文章于 2025-03-10 21:02:51 发布