日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL学习笔记(数值算法)

發布時間:2023/11/30 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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学习笔记(数值算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。