c++ 使用模版函數打印任意容器

使用模版函數打印任任意容器

#include <iostream>
#include <ostream>
using namespace std;

// 打印任意容器
template<typename Contrainer>
void printContrainer(const Contrainer& c, ostream& out = cout)
{
    if (c.empty())
    {
        return;
    }
    else
    {
        typename Contrainer::const_iterator itr = c.begin();
        out << " [ " << *itr++;
        while (itr != c.end())
        {
            out << ", " << *itr++;
        }
        out << " ]" << endl;
    } 
}

代碼路徑: printCollection.h

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章