#include <iostream>
#include <algorithm>
using namespace std;
template <typename S>
struct show
{
void operator()(const S& element) const
{
cout<<element<<endl;
}
};
int main()
{
int a[3]={1,2,3};
for_each(a,a+4,show<int>());
int n;
cin>>n;
return 0;
}
For_each (A, A + 4, show <int> (); how does this work? What is the working principle?
Then I wrote the following struct:
#include <iostream>
#include <algorithm>
using namespace std;
struct Add
{
int sum;
Add()
{
}
Add( int x)
{
sum=x;
}
int operator()(int y)
{
return sum+y;
}
};
int main()
{
int a[5]={1,2,3,4,5};
for_each(a,a+5,Add(1));
Add add= Add(2);
cout<<add(1);
int n;
cin>>n;
return 0;
}
Then I understand that show <int> () is the operator (INT) function that calls the object of the struct.
Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.