Learning notes-template function, template structure, template class

Source: Internet
Author: User
#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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.