现在的位置: 首页 > 综合 > 正文

STL – for_each 简单应用

2013年09月21日 ⁄ 综合 ⁄ 共 385字 ⁄ 字号 评论关闭

http://www.cplusplus.com/reference/algorithm/for_each/

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

template <class T>
void print(T  &t)
{
	cout<<t;
}

template <class T>
class  Print
{
public:
	void operator()(T&t)   
	{   
		cout<<t;   
	}

}; 

int main()
{
	list <int>  L;
	for (int i=1;i <=7;i++)
	   L.push_front(i);
	for_each(L.begin(),L.end(),print<int> );
	cout<<endl;
	for_each(L.begin(),L.end(),Print<int>() );
	return 1;
}


抱歉!评论已关闭.