C++中print函数的模板版本

接受一个数组的引用,能处理任意大小、任意元素类型的数组

#include<iostream>
#include<string>

using namespace std;

template<typename T,size_t N>
void print(const T (&a)[N]) { //接受一个数组的引用,能处理任意大小、任意元素类型的数组
	for (auto iter = begin(a);iter != end(a);++iter) {
		cout << *iter << " ";
	}
	cout << endl;
}

int main() {
	int a[6] = { 2,3,1,5,3,2 };
	string b[3]={ "hello","lucky","how are you" };

	print(a);
	print(b);
	return 0;
}
发布了71 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qionggaobi9328/article/details/105004694