#include <map>
#include <iostream>
#include <string>
using namespace std;
typedef multimap<string,string>::value_type CIT;
typedef multimap<string,string>::const_iterator IT;
template<class T1,class T2>
void show(IT&it,multimap<T1,T2>&r)
{
for (it=r.begin();it!=r.end();++it)
{
cout<<it->first<<"\t"<<it->second<<"\n";
}
}
int main()
{
multimap<string,string>phone;
phone.insert(CIT ("朱然","0108225634"));
phone.insert(CIT("朱然","8708532"));
phone.insert(CIT("朱然","13583852314"));
IT ii;
cout<<"遍历并输出所有元素...\n";
show(ii,phone);
cout<<"查找关键字为“朱然”的所有数据:\n";
//ii=phone.find("朱然");
typedef pair<IT, IT> PAIR;
PAIR p=phone.equal_range("朱然");
for(IT i=p.first; i!=p.second; ++i)
{
cout<<i->first<<"\t"<<i->second<<endl;
}
system("pause");
return 0;
}
#include <iostream>
#include <string>
using namespace std;
typedef multimap<string,string>::value_type CIT;
typedef multimap<string,string>::const_iterator IT;
template<class T1,class T2>
void show(IT&it,multimap<T1,T2>&r)
{
for (it=r.begin();it!=r.end();++it)
{
cout<<it->first<<"\t"<<it->second<<"\n";
}
}
int main()
{
multimap<string,string>phone;
phone.insert(CIT ("朱然","0108225634"));
phone.insert(CIT("朱然","8708532"));
phone.insert(CIT("朱然","13583852314"));
IT ii;
cout<<"遍历并输出所有元素...\n";
show(ii,phone);
cout<<"查找关键字为“朱然”的所有数据:\n";
//ii=phone.find("朱然");
typedef pair<IT, IT> PAIR;
PAIR p=phone.equal_range("朱然");
for(IT i=p.first; i!=p.second; ++i)
{
cout<<i->first<<"\t"<<i->second<<endl;
}
system("pause");
return 0;
}
本文将展示如何利用C++的模板函数特性以及多态性来编写更高效、更简洁的代码。通过具体实例,阐述了如何在不同场景下灵活运用这些高级特性,以提升程序的性能和可维护性。
4317

被折叠的 条评论
为什么被折叠?



