Created
April 16, 2013 19:12
-
-
Save madbence/5398706 to your computer and use it in GitHub Desktop.
Szoftver laboratórium 2, 10. labor, 11. feladat, második próbálkozás
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
class OstreamFunctor { | |
ostream& os; | |
const char* delim; | |
public: | |
ostreamFunctor(ostream& os, const char* delim = ""):os(os),delim(delim){} | |
/** | |
* A const módosító kényelmi okokból van csak ott, | |
* ha akarnánk, lehagyhatnánk. | |
* Viszont akkor konstans objektumon nem működne, mi pedig szeretnénk azt is. | |
*/ | |
void operator()(const T& obj) const { os<<obj<<delim; } | |
}; | |
//Módosítsuk a forEach-et, hogy konstans referenciával tudjunk dolgozni (a fun konstans lett) | |
template<class Iterator, class Function> | |
void forEach(Iterator begin, Iterator end, const Function& fun) { | |
for(;begin!=end;begin++){ | |
fun(*begin); | |
} | |
} | |
//Próbáljuk ki: | |
OstreamFunctor<int> myPrint(cout,"; "); | |
forEach(intarr1.begin(), intarr1.end(), myPrint); | |
//Vagy rövidebben (ezért kellett a konstans referencia) | |
forEach(intarr1.begin(), intarr1.end(), OstreamFunctor<int>(cout,"; ")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment