# Include "stdafx. H"
# Include <iostream>
# Include <vector>
# Include <algorithm>
Using namespace STD;
Class {
Public:
A () {cout <"A ()" <Endl ;}
~ A () {cout <"~ A () "<Endl ;}
Void operator () (Int & K)
{
Cout <k <Endl;
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
{
Vector <int> T (1, 6 );
For_each (T. Begin (), T. End (), ());
}
System ("pause ");
Return 0;
}
A ()
6
~ A ()
~ A ()
~ A ()
~ A ()
Press any key to continue...
The output of the following code is not clear:
Template <class T>
Struct print: Public unary_function <t, void>
{
Print (ostream & out): OS (out), count (0) {cout <"Print ()" <Endl ;}
~ Print () {cout <"~ Print () "<Endl ;}
Void operator () (t x) {OS <x <Endl; ++ count ;}
Ostream & OS;
Int count;
};
Int main ()
{
Int A [] = {1, 4, 2, 8, 5, 7, 22, 99, 55, 33,88 };
Const int n = sizeof (a)/sizeof (INT );
Print <int> P = for_each (A, A + N, print <int> (cout ));
Cout <Endl <p. Count <"objects printed." <Endl;
System ("pause ");
}
The output result is as follows:
Print ()
1
4
2
8
5
7
22
99
55
33
88
~ Print ()
~ Print ()
~ Print ()
11 objects printed.
Press any key to continue...