auto的学习感悟
之前写了一个模板用来输出数组和容器的内容:考虑到数组和链表的不同,只好用了函数重载。
template
void xfy_print(const T& array, int size, const char* space = " ")
{
std::string str_space(space);
for (int i = 0; i < size; ++i)
std::cout << array[i] << str_space;
std::cout << std::endl;
}
template
void xfy_print(const T& vec, const char* space = " ")
{
std::string str_space(space);
for (auto ite = vec.begin(); ite != vec.end(); ++ite)
std::cout << *ite << str_space;
std::cout << std::endl;
}
现在发现用auto就不用重载了
template
void xfy_print(const T& vec, const char* space = " ")
{
std::string str_space(space);
for (auto i : vec)
std::cout << i << str_space;
std::cout << std::endl;
}
而且代码也变简洁了,看来还是很有必要学习C++的新知识。



