函数实例化应用

mark一段函数模板实例化应用的例子:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

template<typename T>
void output(T str);

int main(){
	using std::string;
	using std::vector;
	
	vector<string> vstr = {"zxcv", "asdf", "qwer"};
	for_each(vstr.begin(), vstr.end(), output<const string &>);

	vector<double> vstr2 = {1.0, 2.0, 3.0};
	for_each(vstr2.begin(), vstr2.end(), output<const double &>);

	return 0;
}

template<typename T>
void output(T str){
	using std::cout;
	using std::endl;
	cout << str << endl;
}

主要涉及到一下知识:

  • STL库中for_each函数的使用
  • 函数符的应用
  • 函数模板的显示实例化

猜你喜欢

转载自blog.csdn.net/pix_csdn/article/details/90026333