![]() |
#2
未未来2013-05-29 22:04
|

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
template <typename T>typename T::value_type mostFre(T first,T last){
std::size_t amount=0;
T start=first;
while(start!=last){
amount++;
start++;
}
typedef std::vector<typename T::value_type>VecType;
VecType vec(amount);
VecType::iterator newFirst=vec.begin();
VecType::iterator newlLast=vec.end();
std::uninitialized_copy(first,last,newFirst);
std::sort(newFirst,newlLast);
std::size_t maxOccu=0,occu=0;
VecType::iterator preIter=newFirst;
VecType::iterator maxOccuElmIt=newFirst;
while(newFirst!=newLast){
if(*newFirst!=*preIter){
if(occu>maxOccu){
maxOccu=occu;
maxOccuElmIt=preIter;
}
occu=0;
}++occu;
preIter=newFirst;
++newFirst;
}
if(occu>maxOccu){
maxOccu=occu;
maxOccuElmIt=preIter;
}
return *maxOccuElmIt;
}
int main(){
int ia[]={
1 ,2,3, 4, 5, 1,2,3,1,7
};
vector<int>ve(ia,ia+10);
vector<int>::iterator a=ve.begin(),b=ve.end();
cout<<mostFre(a,b)<<endl;
return 0;
}
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
template <typename T>typename T::value_type mostFre(T first,T last){
std::size_t amount=0;
T start=first;
while(start!=last){
amount++;
start++;
}
typedef std::vector<typename T::value_type>VecType;
VecType vec(amount);
VecType::iterator newFirst=vec.begin();
VecType::iterator newlLast=vec.end();
std::uninitialized_copy(first,last,newFirst);
std::sort(newFirst,newlLast);
std::size_t maxOccu=0,occu=0;
VecType::iterator preIter=newFirst;
VecType::iterator maxOccuElmIt=newFirst;
while(newFirst!=newLast){
if(*newFirst!=*preIter){
if(occu>maxOccu){
maxOccu=occu;
maxOccuElmIt=preIter;
}
occu=0;
}++occu;
preIter=newFirst;
++newFirst;
}
if(occu>maxOccu){
maxOccu=occu;
maxOccuElmIt=preIter;
}
return *maxOccuElmIt;
}
int main(){
int ia[]={
1 ,2,3, 4, 5, 1,2,3,1,7
};
vector<int>ve(ia,ia+10);
vector<int>::iterator a=ve.begin(),b=ve.end();
cout<<mostFre(a,b)<<endl;
return 0;
}
这段代码在vc++6.0上编译出现了 error 2893,目测是因为编译器问题,
同时在cfree上编译也无法通过,
vc++6.0的编译错误信息:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
1.cpp
D:\我的文档\新建文件夹\1.cpp(7) : error C2893: Failed to specialize function template 'generic-type-257 __cdecl mostFre(T,T)'
With the following template arguments:
'int *'
D:\我的文档\新建文件夹\1.cpp(7) : error C2146: syntax error : missing ';' before identifier 'mostFre'
D:\我的文档\新建文件夹\1.cpp(7) : error C2065: 'T' : undeclared identifier
D:\我的文档\新建文件夹\1.cpp(7) : error C2146: syntax error : missing ')' before identifier 'first'
D:\我的文档\新建文件夹\1.cpp(7) : error C2059: syntax error : ')'
执行 cl.exe 时出错.
1.obj - 1 error(s), 0 warning(s)
求老鸟指点看看,,遇到这样的问题很是蛋疼啊,