可能有很大部分人只能看著書本編程,萬一書沒在身邊呢,所以要學會怎麼使用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百度可以下載。