C++模板问题
template <typename Container, typename Function>//这一行创建两个模板我看得懂typename Container::value_type//这一行是什么意思?看不明白
Reduce(const Container& c, Function fn) throw (NotEnoughElements)//这一行这个Reduce函数是哪里的,STL的?
{
}
代码就这么多,求人帮助,谢谢
程序代码:class NotEnoughElements {};
template <typename Container, typename Function>
typename Container::value_type Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
typename Container::const_iterator itor = c.begin();
if( itor == c.end() )
throw NotEnoughElements();
typename Container::value_type result = *itor;
for( ++itor; itor!=c.end(); ++itor )
result = fn( result, *itor );
return result;
}
#include <iostream>
#include <algorithm>
#include <list>
int main( void )
{
std::list<int> con = { 1,2,3,4,5 };
/* 如果用的编译器太古老,改为
std::list<int> con;
con.push_back( 1 );
con.push_back( 2 );
con.push_back( 3 );
con.push_back( 4 );
con.push_back( 5 );
// */
std::cout << Reduce( con, std::plus<int>() ) << std::endl;
return 0;
}
[此贴子已经被作者于2016-2-4 01:50编辑过]