使用模版函數打印任任意容器
#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