编写一个模板函数,找出序列中出现得最频繁的值。。

#include "stdafx.h" #include <iostream> #include <string> #include <map> #include <vector> #include <typeinfo> using namespace std; template <typename Iterator, typename Type> void count_( Iterator first, Iterator last, Type type ) { map<typename iterator_traits<Iterator>::value_type, int> mapc; // iterator_traits 某个类型的iterator所指向的容器保存元素的信息,包括元素的类型,指针类型,引用类型,以便于范型函数中使用这些信息 while(first != last) { pair<map<string,int> ::iterator, bool> ret = mapc.insert(make_pair<typename iterator_traits<Iterator>::value_type,int>(*first,1)); if(!ret.second) ++ret.first->second; first++; } for(map<typename iterator_traits<Iterator>::value_type,int>::iterator it = mapc.begin(); it != mapc.end(); ++it) { cout<<it->first<<" "<<it->second<<endl; } } int main() { vector<string> vs; vs.push_back("aaa"); vs.push_back("aaa"); vs.push_back("aaa"); vs.push_back("bbb"); vs.push_back("bbb"); vs.push_back("bbb"); vs.push_back("bbb"); vs.push_back("ccc"); vs.push_back("ddd"); vs.push_back("ddd"); vs.push_back("ddd"); count_(vs.begin(), vs.end(), string()); map<string,int> ms; ms.insert(make_pair<string,int>("ab",0)); getchar(); return 0; }

你可能感兴趣的:(String,iterator,include)