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