1 // This file is part of the ustl library, an STL implementation.
3 // Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
9 template <typename Container
>
10 void PrintVector (const Container
& ctr
)
13 foreach (typename
Container::const_iterator
, i
, ctr
)
20 A (int dv
= 6) : m_v1 (0), m_v (dv
) {}
21 int addsix (int i
) { return (i
+ m_v
); }
22 void addsix (int& i
) const { i
+= m_v
; }
23 void addtosix (int i
) { m_v
+= i
; }
24 inline void text_write (ostringstream
& os
) const { os
<< m_v
; }
30 INTEGRAL_STREAMABLE(A
)
33 void TestFunctors (void)
38 foreach (vector
<int>::iterator
, i
, v
)
39 *i
-= distance(v
.begin(), i
) & 1;
42 cout
<< "start:\t\t\t";
46 cout
<< "plus:\t\t\t";
47 transform (v
, v
.begin(), v
.begin(), plus
<int>());
51 cout
<< "minus:\t\t\t";
52 transform (v
, v
.begin(), v
.begin(), minus
<int>());
56 cout
<< "divides:\t\t";
57 transform (v
, v
.begin(), v
.begin(), divides
<int>());
61 cout
<< "multiplies:\t\t";
62 transform (v
, v
.begin(), v
.begin(), multiplies
<int>());
66 cout
<< "modulus:\t\t";
67 transform (v
, v
.begin(), v
.begin(), modulus
<int>());
71 cout
<< "logical_and:\t\t";
72 transform (v
, v
.begin(), v
.begin(), logical_and
<int>());
76 cout
<< "logical_or:\t\t";
77 transform (v
, v
.begin(), v
.begin(), logical_or
<int>());
81 cout
<< "equal_to:\t\t";
82 transform (v
, v
.begin(), v
.begin(), equal_to
<int>());
86 cout
<< "not_equal_to:\t\t";
87 transform (v
, v
.begin(), v
.begin(), not_equal_to
<int>());
91 cout
<< "greater:\t\t";
92 transform (v
, v
.begin(), v
.begin(), greater
<int>());
96 cout
<< "less:\t\t\t";
97 transform (v
, v
.begin(), v
.begin(), less
<int>());
101 cout
<< "greater_equal:\t\t";
102 transform (v
, v
.begin(), v
.begin(), greater_equal
<int>());
106 cout
<< "less_equal:\t\t";
107 transform (v
, v
.begin(), v
.begin(), less_equal
<int>());
111 cout
<< "compare:\t\t";
112 transform (v
, v
.begin(), v
.begin(), compare
<int>());
116 cout
<< "negate:\t\t\t";
117 transform (v
, negate
<int>());
121 cout
<< "logical_not:\t\t";
122 transform (v
, logical_not
<int>());
126 cout
<< "unary_neg(negate):\t";
127 transform (v
, unary_negator(negate
<int>()));
131 cout
<< "binder1st(plus,5):\t";
132 transform (v
, bind1st(plus
<int>(), 5));
136 cout
<< "binder2nd(minus,1):\t";
137 transform (v
, bind2nd(minus
<int>(), 1));
141 cout
<< "compose1(-,+5):\t\t";
142 transform (v
, compose1 (negate
<int>(), bind2nd(plus
<int>(), 5)));
146 cout
<< "compose1(-,-4):\t\t";
147 transform (v
, compose1 (negate
<int>(), bind2nd(minus
<int>(), 4)));
151 cout
<< "compose2(/,+6,-4):\t";
152 transform (v
, compose2 (divides
<int>(), bind2nd(plus
<int>(), 6), bind2nd(minus
<int>(), 4)));
155 cout
<< "mem_var(plus,6):\t";
157 for (uoff_t i
= 0; i
< 20; ++ i
)
159 transform (av
, mem_var1(&A::m_v
, bind2nd(plus
<int>(), 6)));
162 vector
<A
>::iterator found
= find_if (av
, mem_var_equal_to(&A::m_v
, 14));
163 cout
<< "14 found at position " << found
- av
.begin() << endl
;
164 found
= lower_bound (av
.begin(), av
.end(), 18, mem_var_less(&A::m_v
));
165 cout
<< "18 found at position " << found
- av
.begin() << endl
;
167 cout
<< "add next:\t\t";
168 transform (av
.begin(), av
.end() - 1, av
.begin() + 1, av
.begin(), mem_var2(&A::m_v
, plus
<int>()));
172 StdBvtMain (TestFunctors
)