首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

模板类习题请问

2013-01-23 
模板类习题请教/*编写一个函数模板,接受表示未知类型迭代器的一对值,找出在序列中出现得最频繁的值。*/#inc

模板类习题请教

/*编写一个函数模板,接受表示未知类型迭代器的一对值,找出在序列中出现得最频繁的值。*/

#include<iostream>
#include<vector>
#include<map>
using namespace std;

template <typename T>  void get_max_val(T b,T e)
{
map<T*,size_t> ret;

for(T it=b;it!=e;++it) // 建立 map 容器:键 - 键出现次数
++ret[*it]; // 编译时此处报错,错误信息见下

size_t maxlen; // 最大出现次数

for(map<T*,size_t>::iterator it=ret.begin();it!=--ret.end();++it) // 找出最大出现次数
{
maxlen = (++it)->second > it->second ? (++it)->second : it->second;
}

for(map<T*,size_t>::iterator it=ret.begin();it!=ret.end() ; ++it)
{
if(it->second==maxlen)
cout<<it->first<<" ";
}
}

int main()
{
int  arr[13]={1,2,2,2,3,3,3,4,4,5,9,9,9};
vector<int> vec(arr,arr+13);
get_max_val(vec.begin(),vec.end());

system("pause");
return 0;
}


error C2679: binary '[' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

请问如何解决?
[解决办法]
++ret[*it]; // 编译时此处报错,错误信息见下
(*it).second++;

热点排行