Skip to content

Instantly share code, notes, and snippets.

@madbence
Created April 16, 2013 19:12
Show Gist options
  • Save madbence/5398706 to your computer and use it in GitHub Desktop.
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
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