//error 函数模板不能有模板默认值,类模板可以
template <typename T = int> void print(int i) { cout<<i<<endl; }


//模板形参T没有作为类型在模板函数的形参表中出现,因此将无法对其进行模板实参推断
template <typename T>
T pp()
{
T a(1);
cout<<"hello"<<endl;
return a;
}
int _tmain(int argc, _TCHAR* argv[])
{
pp();
}



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++;
}
typename std::vector<typename T::value_type> VecType;
VecType vec(amount);
VecType::iterator newFirst = vec.begin();
VecType::iterator newLast = vec.end();
std::uninitialized_copy(first,last,newFirst);
std::sort(newFirst,newLast);
std::size_t maxOccu = 0,occu = 0;
VecType::iterator preIter = newFirst;
VecType::iterator maxOccuElement = newFirst;
while (newFirst != newLast)
{
if (*newFirst != *preIter)
{
if (occu > maxOccu)
{
maxOccu = occu;
maxOccuElement = preIter;
}
occu = 0;
}
++occu;
preIter = newFirst;
++newFirst;
}
if (occu > maxOccu)
{
maxOccu = occu;
maxOccuElement = preIter;
}
return *maxOccuElement;
}
浙公网安备 33010602011771号