64,676
社区成员
发帖
与我相关
我的任务
分享
template <typename T> //T为传递容器的迭代器类型
typename T::value_type mostFre(T first,T last) //value_type指的是元素类型,该程序有错
{
allocator<typename T::value_type> alloc;
T newfirst=alloc.allocate(last-first); //此处错误
T newlast=newfirst+(last-first);
uninitialized_copy(first,last,newfirst);
sort(newfirst,newlast);
size_t maxOcc=0,curOcc=0;
T preIter=newfirst,maxOccit=newfirst;
while(newfirst!=newlast)
{
if(*newfirst!=*preIter)
{
if(curOcc>maxOcc)
{
maxOcc=curOcc;
maxOccit=preIter;
}
curOcc=0;
}
++curOcc;
preIter=newfirst;
++newfirst;
}
if(curOcc>maxOcc)
{
maxOcc=curOcc;
maxOccit=preIter;
}
return *maxOccit;
}
功能为返回一串序列出现的最频繁的值,高手帮看一看怎么不对。
int main()
{
int a[]={1,5,5,4,4,5,2,3};
vector<int> ivec(a,a+8);
vector<int>::iterator it;
int b=mostFre(ivec.begin(),ivec.end());
cout<<b<<endl;
return 0;
}