STL/algorithm/for each
Материал из Wiki.crossplatform.ru
(Различия между версиями)
ViGOur (Обсуждение | вклад) |
ViGOur (Обсуждение | вклад) |
||
Строка 1: | Строка 1: | ||
- | == Подсчет суммы с помощью | + | == Подсчет суммы с помощью объекта функции == |
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
Строка 29: | Строка 29: | ||
<source lang="bash">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sum of v: 120</source> | <source lang="bash">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sum of v: 120</source> | ||
+ | |||
+ | == Расширение объекта функции, нахождение минимума и максимума == | ||
+ | <source lang="cpp"> | ||
+ | #include <iostream> | ||
+ | #include <vector> | ||
+ | #include <algorithm> | ||
+ | #include <functional> | ||
+ | |||
+ | class CMinMax : public std::unary_function<int, void> | ||
+ | { | ||
+ | protected: | ||
+ | bool m_bFirst; | ||
+ | public: | ||
+ | int m_nMin, m_nMax; | ||
+ | public: | ||
+ | CMinMax(): m_nMin(-1), m_nMax(-1), m_bFirst(true){} | ||
+ | void operator()(int nElem) | ||
+ | { | ||
+ | if( m_bFirst) | ||
+ | m_nMin = m_nMax = nElem; | ||
+ | else if (nElem < m_nMin) | ||
+ | m_nMin = nElem; | ||
+ | else if (nElem > m_nMax) | ||
+ | m_nMax = nElem; | ||
+ | m_bFirst = false; | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | std::vector<int> coll; | ||
+ | for( int n = 1; n < 16; ++n) coll.push_back( n); | ||
+ | std::copy( coll.begin(), coll.end(), std::ostream_iterator<int>(std::cout, " ") ); | ||
+ | std::cout << std::endl; | ||
+ | |||
+ | CMinMax mm; | ||
+ | mm = for_each( coll.begin(), coll.end(), mm); | ||
+ | std::cout << "The max is " << mm.m_nMax << std::endl; | ||
+ | std::cout << "The min is " << mm.m_nMin << std::endl; | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="bash"> | ||
+ | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ||
+ | The max is 15 | ||
+ | The min is 1 | ||
+ | </source> |
Версия 12:11, 24 января 2012
Подсчет суммы с помощью объекта функции
#include <iostream> #include <vector> #include <algorithm> #include <functional> class CSum : std::unary_function<int, void> { public: argument_type m_sum; CSum() { m_sum = 0; } result_type operator()(argument_type i) { m_sum += i; } }; int main() { std::vector<int> coll; for( int n = 1; n < 16; ++n) coll.push_back( n); std::copy( coll.begin(), coll.end(), std::ostream_iterator<int>(std::cout, " ") ); CSum s; s = std::for_each( coll.begin(), coll.end(), CSum()); std::cout << "sum of v: " << s.m_sum << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 sum of v: 120
Расширение объекта функции, нахождение минимума и максимума
#include <iostream> #include <vector> #include <algorithm> #include <functional> class CMinMax : public std::unary_function<int, void> { protected: bool m_bFirst; public: int m_nMin, m_nMax; public: CMinMax(): m_nMin(-1), m_nMax(-1), m_bFirst(true){} void operator()(int nElem) { if( m_bFirst) m_nMin = m_nMax = nElem; else if (nElem < m_nMin) m_nMin = nElem; else if (nElem > m_nMax) m_nMax = nElem; m_bFirst = false; } }; int main() { std::vector<int> coll; for( int n = 1; n < 16; ++n) coll.push_back( n); std::copy( coll.begin(), coll.end(), std::ostream_iterator<int>(std::cout, " ") ); std::cout << std::endl; CMinMax mm; mm = for_each( coll.begin(), coll.end(), mm); std::cout << "The max is " << mm.m_nMax << std::endl; std::cout << "The min is " << mm.m_nMin << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 The max is 15 The min is 1