Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板打印功能C++;_C++_Templates_Printing - Fatal编程技术网

C++ 模板打印功能C++;

C++ 模板打印功能C++;,c++,templates,printing,C++,Templates,Printing,到目前为止,我写了以下内容: 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++) {

到目前为止,我写了以下内容:

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);
    }
}
模板
无效打印向量(常量标准::向量和向量)
{
typename std::vector::const_迭代器it;
std::cout
也一样。第二部分没有编译,但这是我解决任务的“想法”。对如何实现这种行为有什么建议吗

编译错误:模板参数列表过多

请删除
模板
部分,函数模板重载将正常工作

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);
    }
}
模板
无效打印向量(常量标准::向量和向量)
{
typename std::vector::const_迭代器it;

std::cout如果您使函数以递归方式打印基类型并覆盖向量:

template<typename T>
void print( const T &t ) 
{ 
    std::cout << t; 
}

template<typename T>
void print( const std::vector<T> &v ) 
{ 
    std::cout << '[';
    for( auto it = v.begin(); it != v.end(); ++it ) {
         if( it != v.begin() ) std::cout << ',';
         print( *it );
    }
    std::cout << ']';
}
模板
无效打印(常量T&T)
{ 

std::cout实际上,您可能希望找到一种更通用的问题解决方案,允许打印几乎任何iterable类型:

#include <vector>
#include <iostream>

template <typename Iterable>
std::ostream& operator<<(std::ostream& os, const Iterable& vals)
{
    for (const auto& val : vals)
        os << val << std::endl;
    return os;
}

int main()
{
    auto simple_vec = std::vector<int>{3, 5 , 7};
    std::cout << simple_vec;
    auto nested_vec = std::vector<std::vector<int>>{{1, 2}, {3, 4}};
    std::cout << nested_vec;
}
#包括
#包括
模板

std::ostream&operator您不能部分地专门化函数,您需要创建一个新的重载。请注意,您的第二个
打印
不会输出额外的
(,)
关于内部向量。OP的版本应该已经处理向量的向量…但您的版本将允许向量列表的向量…:-)您的方法包罗万象。我不建议在没有SFINAE的情况下这样做。实际上我更喜欢OP的版本。而且您忘了输出
(,)
。这正是我最后添加关于SFINAE部分的原因,因为这是默认的攻击方式。