print.h
//print.h#include <iostream>using namespace std;#ifndef print_fun#define print_funtemplate<typename T>///显示序列数据void print(T b,T e,char c=' '){bool isExit=false;while (b!=e){cout<<*b++<<c;isExit=true;}if(isExit) cout<<endl;}template<typename K,typename V>ostream& operator<<(ostream& o,const pair<K,V>& p)//重载输出map类型元素{return o<<p.first<<':'<<p.second;}#endif
set.cpp
/*set特性元素就是key=value不允许重复*/#include<set>#include<iostream>using namespace std;#include<string>#include<fstream>
#include"print.h"int main(){set<string> ss;//定义容器string s;//ifstream fin("maillist.txt");if(!fin){return 1;}while(fin>>s) ss.insert(s);print(ss.begin(),ss.end());}
结果:
multiset.cpp
/*set特性元素就是key=value允许重复key*/#include<set>#include<iostream>using namespace std;#include<string>#include<fstream>#include<map>
#include"print.h"int main(){multiset<string> ms;string s;ifstream fin("maillist.txt");if(!fin){return 1;}while(fin>>s) ms.insert(s);print(ms.begin(),ms.end());multiset<string>::iterator ib=ms.begin(),ie;multimap<int,string> mis;while(ib!=ms.end()){mis.insert(make_pair(ms.count(*ib),*ib));//排好序的一段ib=ms.upper_bound(*ib);}print(mis.begin(),mis.end());return 0;}
maillist.txt
ppp1@qq.comppp2@qq.comppp3@qq.comppp2@qq.comppp2@qq.comppp1@qq.comppp1@qq.com
结果: