1: //adjacent_difference2a.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 " 25: "adjacent_difference() algorithm\n(extended version) from " 26: "<numeric> to find the value of the programmer-defined" 27: "\nfunction f(a,b)=2*a+b applied to successive pairs of " 28: "integers in one vector,\nand write these values to a second " 29: "vector."; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: vector<int> v1{2, 4, 6, 8, 10}; 33: cout << "\nHere are the values in the first vector:\n"; 34: for (int i : v1) cout << i << " "; //since C++11 35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 37: vector<int> v2{1, 3, 5, 7, 9, 11, 13}; 38: cout << "\nAnd here are the values in the second vector:\n"; 39: for (int i : v2) cout << i << " "; //since C++11 40: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 42: cout << "\nNext a call to ajacent_difference computes the " 43: "function 2a+b for all integer\npairs in the first vector, " 44: "and writes the values to the second vector, starting\n" 45: "at the second position of the second vector."; 46: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 48: //since C++11 ... 49: auto p = adjacent_difference(v1.begin(), v1.end(), v2.begin()+1, f); 50: cout << "\nHere are the integer values in the second vector " 51: "after this has been done:\n"; 52: for (int i : v2) cout << i << " "; //since C++11 53: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 54: cout << "\nAnd here is the value pointed to by the iterator " 55: "returned by the algorithm: "; 56: cout << *p; 57: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 58: }