對照msdn函數原型,實現函數功能之transform(教你如何查文檔)

來源:互聯網
上載者:User

可能有很大部分人只能看著書本編程,萬一書沒在身邊呢,所以要學會怎麼使用MSDN,雖然裡面解釋大多是英文,只要理解了其中的參數,就迎刃而解了。

這裡是直接從MSDN複製過來的,以transform為例:

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 eachN in the range[0, last - first). It then returnsx + (last - first).The
call uop(*(first + N)) must not alter*(first + N).

The second template function evaluates *(x + N) = bop(*(first1 + N), *(first2 + N)) once for eachN in the range[0, last1 - first1). It then returnsx + (last1 - first1).The
call bop(*(first1 + N), *(first2 + N)) must not alter either*(first1 + N) or*(first2 + N).

 

1首先我們看到兩個模板函數,關於這兩個模板函數的解釋,下面都有英文解釋,英文不太懂的,用線上翻譯或其他翻譯工具。其實,也不用十分懂,關鍵單詞知道意思就行了。

2解釋下參數的意思:

InIt表示輸入參數  OutIt表示輸出參數,Unop表示一元函數,Binop表示二元函數

InIt一般用數組或指標又或者是迭代器,OutIt用來裝載結果的迭代器(或數組,指標)首址。

3第一個函數跟第二個函數最大的不同是,參數不同,The calluop(*(first + N)) must not alter*(first + N).不允許改變first迭代器所指容器的值。The callbop(*(first1 + N), *(first2
+ N))
must not alter either*(first1 + N) or*(first2 + N).不允許改變first1和first2迭代器所指容器的值。所以,第一函數的值只能有first2迭代器所指的容器去接收值,而第二個函數只能用第三方迭代器所指的容器去接收值。

4看具體代碼:

第一個函數的代碼:

#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;}

 

 

第二函數的代碼:

#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;}

 

趕快去試試別的函數吧,MSDN2001 OTC百度可以下載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.