1: //function_objects_stl.cpp
2: //Illustrates several STL built-in function objects.
4: #include <iostream>
5: #include <iomanip>
6: #include <string>
7: #include <vector>
8: #include <algorithm>
9: #include <iterator>
10: #include <functional>
11: using namespace std;
13: int main()
14: {
15: cout << "\nThis program illustrates several STL function objects.";
16: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
18: multiplies<int> f;
19: cout << "\nThe product of 25 and 6 is " << f(25, 6) << ".";
20: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
22: //The above is a short form for the following:
23: cout << "\nThe product of 12 and 8 is "
24: << multiplies<int>().operator()(12, 8) << ".";
25: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
27: multiplies<double> g;
28: cout << "\nThe product of 3.14 and 2.71 is " << g(3.14, 2.71) << ".";
29: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
31: less<string> h;
32: cout << "\nThe expression (\"Hello\" < \"Good-bye\") is "
33: << boolalpha << h("Hello", "Good-bye") << ".";
34: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
36: int a[] = {1, 2, 3, 4, 5};
37: vector<int> v(a, a+5);
38: cout << "\nHere are the contents of an integer vector:\n";
39: copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
40: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
41: cout << "\nNow we negate all the values and re-display to confirm:\n";
42: transform(v.begin(), v.end(), v.begin(), negate<int>());
43: copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
44: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
45: }