This shows you the differences between two versions of the page.
| Next revision Both sides next revision | |||
|
cpp:functors [2020/05/31 20:32] gthanos created |
cpp:functors [2020/05/31 20:34] gthanos |
||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ===== Η συνάρτηση for_each ===== | ===== Η συνάρτηση for_each ===== | ||
| - | Η συνάρτηση [[http:// | + | Η συνάρτηση [[http:// |
| + | <code cpp> | ||
| + | template< | ||
| + | Function for_each(InputIterator first, InputIterator last, Function fn) { | ||
| + | while (first!=last) { | ||
| + | fn (*first); | ||
| + | ++first; | ||
| + | } | ||
| + | return fn; // or, since C++11: return move(fn); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Παρακάτω δίνεται ένα παράδειγμα, | ||
| <code cpp prefix_str.cpp> | <code cpp prefix_str.cpp> | ||
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| using namespace std; | using namespace std; | ||
| Line 53: | Line 60: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| using namespace std; | using namespace std; | ||