1: //accumulate2a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <numeric> 6: using namespace std; 8: /** 9: Finds the product of two integers. 10: Pre:\n a and b contain integer values. 11: Post:\n The product of a and b has been returned. 12: */ 13: int product 14: ( 15: int a, //in 16: int b //in 17: ) 18: { 19: return a*b; 20: } 22: int main() 23: { 24: cout << "\nThis program illustrates the use of the STL accumulate() " 25: "algorithm (expanded\nversion) from <numeric> to find the " 26: "product of integers in a vector, using a\nprogrammer-defined " 27: "function to compute the product of two integers."; 28: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 30: vector<int> v{2, 4, 6, 8, 10}; //since C++11 31: cout << "\nHere are the integer values in the vector:\n"; 32: for (int i : v) cout << i << " "; //since C++11 33: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 35: cout << "\nThe product of these values is " 36: << accumulate(v.begin(), v.end(), 1, product) << "."; 37: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 39: cout << "\nThe product of the middle three values and an intial " 40: "value of -5 is " 41: << accumulate(v.begin()+1, v.end()-1, -5, product) << ". "; 42: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 44: cout << "\nThe product of the values in an empty range, with " 45: "an intial value of 22, is " 46: << accumulate(v.begin(), v.begin(), 22, product) << "."; 47: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 48: }