第十五周實踐專案1 3 閱讀程式,體會STL用法

2021-07-13 05:30:29 字數 1245 閱讀 2467

問題及**:

/*

*all right reserved.

*檔名稱:77.cpp

*作 者:董凱琦

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

*版 本 號:v1.0

* *問題描述:

閱讀程式,體會stl用法

*輸入描述:

*程式輸出:

*/#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執行結果:

程式分析:

1、sizeof a表示陣列a的位元組數,sizeof a[0]表示a[0]的位元組數,故sizeof a/size of a[0]表示陣列a裡元素的個數

2、構建輸出迭代器out,將陣列a,陣列b裡的元素依次輸出

3、求和,並輸出結果

4、inner_product:返回作為兩個序列乘積而生成的元素的總和。步調一致地檢查兩個序列,將

來自兩個序列的元素相乘,將相乘的結果求和。由 init 指定和的初值。 

第十五周專案 閱讀程式2

all right reserved.檔名稱 test.cpp 完成日期 2016年6月21日 版本號 v1.0 問題描述 閱讀程式 程式輸入 程式輸出 include include include using namespace std class cstudent cstudent int n...

第十五周 閱讀程式(1)

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

第十五周 閱讀程式(2)

檔名稱 test.cpp 作 者 呼亞萍 完成日期 2015年6月15日 版 本 號 v1.0 問題描述 檢視下面程式的輸出,解釋為什麼會有這樣的輸出。程式輸入 相應的程式 程式輸出 對應得結果 include include using namespace std int main ofstrea...