Implement the function transform based on the msdn function prototype (how to query the document)

Source: Internet
Author: User

Most people may have to look at book programming. If the book is not around, they should learn how to use msdn. Although most of the explanations are in English, they should be able to understand the parameters.

Here we copy it directly from msdn. Take transform as an example:

Transform
template<class InIt, class OutIt, class Unop>    OutIt transform(InIt first, InIt last, OutIt x, Unop uop);template<class InIt1, class InIt2, class OutIt, class Binop>    OutIt transform(InIt1 first1, InIt1 last1, InIt2 first2,        OutIt x, Binop bop);

The first template function evaluates*(x + N) = uop(*(first + N))Once for eachNIn the range[0, last - first). It then returnsx + (last - first).
Calluop(*(first + N))Must not alter*(first + N).

The second template function evaluates*(x + N) = bop(*(first1 + N), *(first2 + N))Once for eachNIn the range[0, last1 - first1). It then returnsx + (last1 - first1).
Callbop(*(first1 + N), *(first2 + N))Must not alter either*(first1 + N)Or*(first2 + N).

 

1. First, we can see two template functions. The explanations for these two template functions are as follows. If you are not familiar with English, you can use online translation or other translation tools. In fact, you don't need to understand the meaning of key words.

2. explain the meaning of the following parameters:

Init indicates the input parameter outit indicates the output parameter, UNOP indicates the unary function, and binop indicates the binary function.

Init generally uses arrays, pointers, or iterators. outit is used to load the first address of the result iterator (OR array, pointer.

3. The biggest difference between the first function and the second function is that the parameter is different. The calluop(*(first + N))Must not alter*(first + N). The Container value specified by the first iterator cannot be changed. The callbop(*(first1 + N), *(first2
+ N))
Must not alter either*(first1 + N)Or*(first2 + N). Changing the container value specified by the first1 and first2 iterators is not allowed. Therefore, the value of the first function can only be received by the container referred to by the first2 iterator, while the second function can only receive the value by the container referred to by the third-party iterator.

4. Check the specific code:

Code of the first function:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;int chenger(int a){ return 2*a; }int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; vector<int>v(a,a+10); vector<int>vv(10); transform(v.begin(),v.end(),vv.begin(),chenger); for(vector<int>::iterator iter=vv.begin();iter!=vv.end();++iter){  cout<<*iter<<" "; } cout<<endl; system("pause"); return 0;}

 

 

Code of the second function:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;int gaibian(int a,int b){ return a+b;}int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; int b[]={100,200,300,400,500,600,700,800,900,1000}; vector<int>v(a,a+10); vector<int>vv(b,b+10); vector<int>vvv(10); transform(v.begin(),v.end(),vv.begin(),vvv.begin(),gaibian); for(vector<int>::iterator iter=vvv.begin();iter!=vvv.end();++iter){  cout<<*iter<<" "; } cout<<endl; system("pause"); return 0;}

 

Hurry up and try other functions. You can download them from msdn2001 OTC Baidu.

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.