1: //partial_sum2a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <numeric> 6: using namespace std; 8: /** 9: Evaluates the expression 2a+b. 10: Pre:\n a and b contain integer values. 11: Post:\n Returns the value 2a+b. 12: */ 13: int f 14: ( 15: int a, //in 16: int b //in 17: ) 18: { 19: return 2*a + b; 20: } 22: int main() 23: { 24: cout << "\nThis program illustrates the use of the STL partial_sum() " 25: "algorithm (extended\nversion) from <numeric> to find the \"running " 26: "totals\" of integer values stored\nin a vector of integers and " 27: "computed using\n\nnewOutputValue = 2 * oldOutputValue + " 28: "newInputValue\n\nand to write those computed values out to the " 29: "same vector of integers."; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: int a[] = {1, 2, 3, 4, 5}; 33: vector<int> v(a, a+5); 35: cout << "\nHere are the initial values in the vector:\n"; 36: for (vector<int>::size_type i=0; i<v.size(); i++) 37: cout << v.at(i) << " "; 38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 40: vector<int>::iterator p = partial_sum(v.begin(), v.end(), v.begin(), f); 41: cout << "\nAnd here is the same vector after the \"partial sums\"" 42: "\nhave been computed and the original values overwritten:\n"; 43: for (vector<int>::size_type i=0; i<v.size(); i++) 44: cout << v.at(i) << " "; 45: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 47: cout << "\nThe iterator p returned by the algorithm points to "; 48: if (p == v.end()) 49: cout << "\nthe end of the ouput container."; 50: else 51: cout << "the value " << *p << "."; 52: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 53: }