📄 example_13_22.cpp
字号:
//Numeric algorithms accumulate and adjacent_difference
#include <iostream>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <vector>
#include <functional>
using namespace std;
void print(vector<int> vList);
int main()
{
int list[8] = {1, 2, 3, 4, 5, 6, 7, 8}; //Line 1
vector<int> vecList(list, list + 8); //Line 2
vector<int> newVList(8); //Line 3
cout<<"Line 4: vecList: "; //Line 4
print(vecList); //Line 5
//accumulate
int sum = accumulate(vecList.begin(),
vecList.end(), 0); //Line 6
cout<<"Line 7: Sum of the elements of vecList = "
<<sum<<endl; //Line 7
int product = accumulate(vecList.begin(),
vecList.end(),
1, multiplies<int>()); //Line 8
cout<<"Line 9: Product of the elements of "
<<"vecList = "<<product<<endl; //Line 9
//adjacent_difference
adjacent_difference(vecList.begin(),
vecList.end(),
newVList.begin()); //Line 10
cout<<"Line 11: newVList: "; //Line 11
print(newVList); //Line 12
adjacent_difference(vecList.begin(), vecList.end(),
newVList.begin(),
multiplies<int>()); //Line 13
cout<<"Line 14: newVList: "; //Line 14
print(newVList); //Line 15
return 0;
}
void print(vector<int> vList)
{
ostream_iterator<int> screenOut(cout, " "); //Line 16
copy(vList.begin(), vList.end(), screenOut); //Line 17
cout<<endl; //Line 18
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -