因为c++没有打印vector的方法,所以为了便于一些数据的显示,想写个通用的print函数,通用当然是想到用模板啦,我写了个简单函数如下:
template <typename T>
void print(vector<T> a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] <<" ";
}
cout << endl;
}
template <typename T>
void print2(vector<T> a)
{
vector<T>::iterator it;
for (it= a.begin(); it != a.end(); it++)
{
cout << *it << endl;
}
}
其中print可以正常使用,print2编译都不通过,老是报error: ‘it’ was not declared in this scope,具体原因也不知道,大概是类型实例化是造成的.