C++ primer中的一道习题(16.12),该怎么处理
C++ primer中的一道习题(16.12)
题目:
编写一个函数模版,接受表示未知类型迭代器的一对值,找出在序列中出现的最频繁的值。
该如何定义迭代器?
//16.12
#include<iostream>
#include<string>
#include<vector>
#include<list>
using namespace std;
template<typename T, typename V>
V most_occurs(typename T::const_iterator beg, typename T::const_iterator end)
{
typename T::size_type sz=0,max=0;
V tmp;
for(typename T::const_iterator it=beg; it!=end; ++it)
{
sz=count(beg,end,*it);
if(sz>max)
{
max=sz;
tmp=*it;
}
}
return tmp;
}
int main()
{
vector<int> vec;
int i;
for(i=0;i<10;++i)
vec.push_back(i);
vec.push_back(2);
vec.push_back(2);
vector<int>::const_iterator beg=vec.begin(), end=vec.end();
cout<<most_occurs(beg,end )<<endl;
return 0;
}
错误信息如下:
error C2783: 'V __cdecl most_occurs(generic-type-311,generic-type-312)' : could not deduce template argument for 'T'
error C2783: 'V __cdecl most_occurs(generic-type-311,generic-type-312)' : could not deduce template argument for 'V'
请问各位高手该如何定义该template?谢谢^_^
------解决方案--------------------
题目:
编写一个函数模版,接受表示未知类型迭代器的一对值,找出在序列中出现的最频繁的值。
该如何定义迭代器?
//16.12
#include<iostream>
#include<string>
#include<vector>
#include<list>
using namespace std;
template<typename T, typename V>
V most_occurs(typename T::const_iterator beg, typename T::const_iterator end)
{
typename T::size_type sz=0,max=0;
V tmp;
for(typename T::const_iterator it=beg; it!=end; ++it)
{
sz=count(beg,end,*it);
if(sz>max)
{
max=sz;
tmp=*it;
}
}
return tmp;
}
int main()
{
vector<int> vec;
int i;
for(i=0;i<10;++i)
vec.push_back(i);
vec.push_back(2);
vec.push_back(2);
vector<int>::const_iterator beg=vec.begin(), end=vec.end();
cout<<most_occurs(beg,end )<<endl;
return 0;
}
错误信息如下:
error C2783: 'V __cdecl most_occurs(generic-type-311,generic-type-312)' : could not deduce template argument for 'T'
error C2783: 'V __cdecl most_occurs(generic-type-311,generic-type-312)' : could not deduce template argument for 'V'
请问各位高手该如何定义该template?谢谢^_^
------解决方案--------------------
- C/C++ code
#include <iostream> #include <string> #include <vector> #include <iterator> #include <algorithm> #include <list> using namespace std; template <typename T> typename iterator_traits<T>::value_type most_occurs( T beg, T end ) { typename iterator_traits<T>::difference_type sz = 0,max = 0; typename iterator_traits<T>::value_type tmp; for ( T it = beg; it != end; ++it ) { sz = count( beg, end, *it ); if ( sz > max ) { max = sz; tmp = *it; } } return tmp; } int main() { vector <int> vec; int i; for ( i = 0; i < 10; ++i ) { vec.push_back( i ); } vec.push_back( 2 ); vec.push_back( 2 ); vector <int> ::const_iterator beg = vec. begin(), end = vec.end(); cout << most_occurs( beg, end ) << endl; return 0; }
------解决方案--------------------
- C/C++ code
template <typename T> typename T::value_type mostFre(T first,T last) { allocator<typename T::value_type>alloc; T newFirst=alloc.allocate(last-first); T newLast =newFirst +(last-first); std::uninitialized_copy(first,last,newFirst); std::sort (newFirst,newLast); std::size_t maxOccu =0,occ=0; T preIter =newFirst,maxOccuElemIt =newFirst; while(newFirst!=newLast) { if(*newFirst!=*preIter) { if(occu>maxOccu) { maxOccu=occu; maxOccuElemIt=preIter; } occu=0; } ++occu; preIter=newFirst; ++newFirst; } if(occu>maxOccu) { maxOccu=occu; maxOccuElemIt=preIter; } return *maxOccuElemIt; }
------解决方案--------------------
最简单的做法:cout <<most_occurs<vector<int>,int>(beg,end) <<endl; 呵呵...
C++中要求在模板函数调用时,必须能够推导出参数类型(可以递归使用推导规则).
楼主的做法中,嵌套类型vector <int> ::const_iterator很明显不可能为其"外嵌"类型负责啦...