第十五周閱讀程式 3

2021-07-13 03:07:17 字數 1485 閱讀 3445

* 檔名稱:text.cpp

* 完成日期:2023年6月5日

* 版本號:vc++6.0

* 問題描述:閱讀下面程式,並寫出執行結果

*/#include #include #include #include #include #include using namespace std;

int main()

; int b = ;

const int asz = sizeof a / sizeof a[0];

const int bsz = sizeof b / sizeof b[0];

ostream_iteratorout(cout," ");

copy(a, a + asz, out);

cout

cout << "accumulate 2: " << r << endl;

r = inner_product(a, a + asz, b, 0);

// 或 r = inner_product(a, a + asz, b, 0, plus(), multiplies());

cout << "inner_product : " << r << endl;

int* it = partial_sum(a, a + asz, b);

// 或 int* it = partial_sum(a, a + asz, b, plus());

copy(b, it, out);

cout

copy(b, it, out);

cout<

執行結果;

過程分析:
out為輸出流的乙個物件,在copy語句中直接輸出a,b陣列的元素。accumulate是求和演算法,頭兩個形參指定要累加的元素範圍,第三個形參則是累加的初值。
所以accumulate1,2輸出的是a,b陣列元素的和。inner_product返回作為兩個序列乘積而生成的元素的總和。步調一致地檢查兩個序列,將來自兩個序列的元素相乘,將相乘的結果求和。由 init 指定和的初值。假定從beg2 開始的第二個序列具有至少與第乙個序列一樣多的元素,忽略第二個序列中超出第乙個序列長度的任何元素。init 的型別決定返回型別。這裡a和b的長度一樣,所以輸出每個對應元素乘積的和。pattial_sum函式作用是把n項相加賦值給n。所以1+4=5,1+4+7=12........

對於給定的序列x0,x1,...,x(n-1),計算序列中相鄰兩個元素的差序列x1-x0,x2-x1,...,x(n-1)-x(n-2)。該可以把結果序列儲存在原序列中,也可以儲存在另乙個區間中。所以就是4-1,7-4...



第十五周 閱讀程式(3)

檔名稱 test.cpp 作 者 呼亞萍 完成日期 2015年6月15日 版 本 號 v1.0 問題描述 閱讀下面的程式,指出其功能,體會seekg tellg 等函式的功能及其用法 程式輸入 相應的程式 程式輸出 對應得結果 include include using namespace std ...

第十五周閱讀程式(3)

問題及 檔名稱 zwj.cpp 作 者 張偉晶 完成日期 2016年6月3日 版 本 號 v1.0 問題描述 閱讀程式,對照執行結果,領會stl的用法 輸入描述 程式輸出 include include include include include include using namespace ...

第十五周 閱讀程式(1)

檔名稱 test.cpp 作 者 呼亞萍 完成日期 2015年6月15日 版 本 號 v1.0 程式輸入 相應的程式 程式輸出 對應得結果 include include include using namespace std int main int a ofstream outfile f1.d...