STL学习笔记(数值算法)
運用數值算法之前必須先加入頭文件<numeric>
?
加工運算后產生結果
1.對序列進行某種運算
T
accumulate(InputIterator beg,InputIterator end,
? ? ? ? ? ? ? ? ? ?T initValue)
T?
accumulate(InputIterator beg,InputIterator end,
? ? ? ? ? ? ? ? ? ?T initValue,BinaryFunc op)
1.第一種形式計算InitValue和區間[beg,end)內所有元素的總和。
2.第二種形式計算initValue和區間[beg,end)內每一個元素進行op運算的結果。更具體的說,它針對每一個元素調用以下表達式:
? ?initValue=op(initValue,elem)
?
下面這個例子展示如何使用accumulate()得到區間內所有元素的總和和乘積:
1 #include "algostuff.hpp" 2 using namespace std; 3 4 int main() 5 { 6 vector<int> coll; 7 INSERT_ELEMENTS(coll,1,9); 8 PRINT_ELEMENTS(coll); 9 cout<<"sum: " 10 <<accumulate(coll.begin(),coll.end(),0) 11 <<endl; 12 cout<<"sum: " 13 <<accumulate(coll.begin(),coll.end(),-100) 14 <<endl; 15 cout<<"product: " 16 <<accumulate(coll.begin(),coll.end(),1,multiplies<int>()) 17 <<endl; 18 cout<<"product: " 19 <<accumulate(coll.begin(),coll.end(),0,multiplies<int>()) 20 <<endl; 21 } View Code?
2.計算兩序列的內積
T
inner_product(InputIterator beg1,InputIterator end1,
? ? ? ? ? ? ? ? ? ? ? ?InputIterator beg2,T initValue)
T
inner_product(InputIterator beg1,InputIterator end1,
? ? ? ? ? ? ? ? ? ? ? ?InputIterator beg2,T initValue,
? ? ? ? ? ? ? ? ? ? ? ?BinaryFunc op1,BinaryFunc op2)
1.第一種形式針對“兩區間內的每一組對應元素”調用以下表達式:
? ?initValue=initValue+elem1+elem2
2.第二形式則調用以下表達式:
? initValue=op1(initValue,op2(elem1,elem2))
?
以下程序示范inner_product()的用法
1 #include "algostuff.hpp" 2 using namespace std; 3 4 int main() 5 { 6 list<int> coll; 7 INSERT_ELEMENTS(coll,1,6); 8 PRINT_ELEMENTS(coll); 9 cout<<"inner product: " 10 <<inner_product(coll.begin(),coll.end(),coll.begin(),0) 11 <<endl; 12 cout<<"inner reverse product: " 13 <<inner_product(coll.begin(),coll.end(),coll.rbegin(),0) 14 <<endl; 15 cout<<"product of sums: " 16 <<inner_product(coll.begin(),coll.end(),coll.begin(),1,multiplies<int>(),plus<int>()) 17 <<endl; 18 } View Code?
?
相對值跟絕對值之間的轉換
1.將相對值轉換成絕對值
OutputIterator
partial_sum(InputIterator sourceBeg,
? ? ? ? ? ? ? ? ? ? InputIterator sourceEnd,
? ? ? ? ? ? ? ? ? ? OutputIterator destBeg)
OutputIterator
partial_sum(InputIterator sourceBeg,
? ? ? ? ? ? ? ? ? ? InputIterator sourceEnd,
? ? ? ? ? ? ? ? ? ? OutputIterator destBeg,BinaryFunc op)
1.第一形式計算源區間[sourceBeg,sourceEnd)中每個元素的部分和,然后將結果寫入以destBeg為起點的目標區間
2.第二形式將源區間[sourceBeg,sourceEnd)中的每個元素和其先前所有元素進行op運算,并將結果寫入destBeg為起點的目標區間
? ?例如對于以下數值序列:a1 a2 a3 ...
? ?它們分別計算:
? ?a1,a1+a2,a1+a2+a3,..
? ?a1,a1 op a2,a1 op a2 op a2,...
以下程序示范partial_sum()的用法
1 #include <iterator> 2 #include "algostuff.hpp" 3 using namespace std; 4 5 int main() 6 { 7 vector<int> coll; 8 INSERT_ELEMENTS(coll,1,6); 9 PRINT_ELEMENTS(coll); 10 partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," ")); 11 cout<<endl; 12 partial_sum(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>()); 13 cout<<endl; 14 } View Code?
?
2.將絕對值轉換成相對值
OutputIterator
adjacent_difference(InputIterator sourceBeg,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? InputIterator sourceEnd,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? OutputIterator destBeg)
OutputIterator
adjacent_difference(InputIterator sourceBeg,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? InputIterator sourceEnd,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? OutputIterator destBeg)
1.第一種形式計算區間[sourceBeg,sourceEnd)中每一個元素和其緊鄰前驅元素的差額,并將結果寫入destBeg為起點的目標區間
2.第二種形式針對區間[sourceBeg,sourceEnd)中每一個元素和其緊鄰前驅元素調用op操作,并將結果寫入destBeg為起點的目標區間
? ?對于以下數值序列:
? ?a1,a2,a3,a4,...
? ?它們分別計算:
? ?a1,a2-a1,a3-a2,a4-a3,...
? ?a1,a2 op a1,a3 op a2,a4 op a3,...
以下程序示范adjacent_difference()的用法
1 #include <iterator> 2 #include "algostuff.hpp" 3 using namespace std; 4 5 int main() 6 { 7 deque<int> coll; 8 INSERT_ELEMENTS(coll,1,6); 9 PRINT_ELEMENTS(coll); 10 adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),plus<int>()); 11 cout<<endl; 12 adjacent_difference(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),multiplies<int>()); 13 cout<<endl; 14 } View Code?
轉載于:https://www.cnblogs.com/runnyu/p/4849852.html
總結
以上是生活随笔為你收集整理的STL学习笔记(数值算法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 图像处理(从安装Pillo
- 下一篇: SPOJ SORTBIT Sorte