Dar*_*agh 3 c++ printing templates
到目前为止我写了这个:
template <typename TType>
void print_vector(const std::vector<TType>& vec)
{
typename std::vector<TType>::const_iterator it;
std::cout << "(";
for(it = vec.begin(); it != vec.end(); it++)
{
if(it!= vec.begin()) std::cout << ",";
std::cout << (*it);
}
std::cout << ")";
}
template<>
template <typename T2>
void print_vector(const std::vector< std::vector<T2> >& vec)
{
for( auto it= vec.begin(); it!= vec.end(); it++)
{
print_vector(*it);
}
}
Run Code Online (Sandbox Code Playgroud)
第一个函数适用于诸如此类的事情std::vector< double>.现在我想要能够打印std::vector< std::vector< TType>>东西.第二部分没有编译,但这是我解决任务的"想法".关于如何实现这种行为的任何建议?
Compilation Error: too many template-parameter-lists
删除template<>部分,功能模板重载将正常工作.
template <typename TType>
void print_vector(const std::vector<TType>& vec)
{
typename std::vector<TType>::const_iterator it;
std::cout << "(";
for(it = vec.begin(); it != vec.end(); it++)
{
if(it!= vec.begin()) std::cout << ",";
std::cout << (*it);
}
std::cout << ")";
}
template <typename T2>
void print_vector(const std::vector< std::vector<T2> >& vec)
{
for( auto it= vec.begin(); it!= vec.end(); it++)
{
print_vector(*it);
}
}
Run Code Online (Sandbox Code Playgroud)