37 void SimpleRandInit(C& c)
40 for (
auto it = c.begin(); it != c.end(); ++it) {
47 typename C::value_type Sum(
const C& c)
49 typename C::value_type val =
typename C::value_type();
50 for (
auto it = c.cbegin(); it != c.cend(); ++it) {
58 std::map<typename C::value_type, unsigned int> Bin(C
const& c)
60 std::map<typename C::value_type, unsigned int> m;
61 for (
auto it = c.cbegin(); it != c.cend(); ++it) {
68 template <
typename It>
69 typename It::value_type Sum(It first, It last)
71 typename It::value_type val =
typename It::value_type();
72 for (It it = first; it != last; ++it) {
79 template <
typename It>
80 std::map<typename It::value_type, unsigned int> Bin(It first, It last)
82 std::map<typename It::value_type, unsigned int> m;
83 for (It it = first; it != last; ++it) {
95 explicit Accumulator(T t = T()) : m_accum(t) {}
98 T operator()(T t) {
return (m_accum += t); }
101 T operator()()
const {
return m_accum; }
112 explicit Incrementor(
unsigned int n) : m_num(n) {}
115 template <
typename T>
118 for (
unsigned int i = 1; i <= m_num; i++) {
129 template <
typename T,
typename U>
130 std::ostream& operator<<(std::ostream& out, std::pair<T, U> p)
133 (out.operator<<(p.second)).operator<<(endl);
138 template <
typename C>
139 std::ostream& Printer(std::ostream& out, C
const& c)
141 for (
auto it = c.cbegin(); it != c.cend(); ++it) {
147 template <
typename T>
148 std::ostream& operator<<(std::ostream& os, std::vector<T>
const& v)
150 return Printer<std::vector<T>>(os, v);
153 template <
typename T>
154 std::ostream& operator<<(std::ostream& os, std::list<T>
const& v)
156 return Printer<std::list<T>>(os, v);
159 template <
typename T,
typename U>
160 std::ostream& operator<<(std::ostream& os, std::map<T, U>
const& v)
162 return Printer<std::map<T, U>>(os, v);
169 string marker =
"\n------------------------------------\n";
170 cout << std::boolalpha;
173 cout << marker <<
"simpleRandInit for vector of 10 int:" << endl;
174 std::vector<double> v(10);
176 cout << v << endl <<
"sum: " << Sum(v) << endl;
180 cout << marker <<
"bin processes list of 15 int:" << endl;
181 std::list<int> v(15);
183 cout << v << endl <<
"sum: " << Sum(v.cbegin(), v.cend()) << endl << Bin(v) << endl;
187 cout << marker <<
"bin processes vector of 10 double:" << endl;
188 std::vector<double> v(10);
189 generate(v.begin(), v.end(),
RandInt(7, static_cast<unsigned int>(v.size())));
190 cout << v << endl << Bin(v.begin(), v.end()) << endl;
194 cout << marker <<
"find in list of 15 int:" << endl;
195 std::list<int> v(15);
198 auto it1 = find(v.cbegin(), v.cend(), 6);
199 auto it2 = find(it1, v.cend(), 14);
200 auto it3 = find(it1, it2, 13);
201 cout << (it3 != it2) << endl;
205 cout << marker <<
"functor to accumulate sequence:" << endl;
206 std::vector<int> v(10);
207 std::list<int> l(12);
208 generate(v.begin(), v.end(),
RandInt(8, static_cast<unsigned int>(v.size())));
209 generate(l.begin(), l.end(),
RandInt(6, static_cast<unsigned int>(v.size())));
211 a = for_each(v.begin(), v.end(), a);
212 cout << v << endl << a() << endl;
214 for_each(l.begin(), l.end(), b);
215 cout << l << endl << a() << endl;
219 cout << marker <<
"functor to transform sequence:" << endl;
220 std::vector<int> v(8);
222 generate(v.begin(), v.end(),
RandInt(3, static_cast<unsigned int>(v.size())));
223 cout <<
"transform out of place: " << endl;
224 transform(v.begin(), v.end(), l.begin(), Incrementor(2));
225 cout << v << endl << l << endl;
226 cout <<
"transform in place: " << endl;
227 transform(v.begin(), v.end(), v.begin(), Incrementor(5));
std::ostream & operator<<(std::ostream &out, std::pair< T, U > const &p)
Produce random integer (int) values.
Functor produces random values.