Skip to content

Instantly share code, notes, and snippets.

@emadflash
Created June 29, 2021 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emadflash/42e74a04df1bbc6a300fb5b7add1f0da to your computer and use it in GitHub Desktop.
Save emadflash/42e74a04df1bbc6a300fb5b7add1f0da to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
template<typename It, typename OutIt>
OutIt printContainer(It begin, It end, OutIt ot) {
for(auto it=begin; it != end; ++it) {
*ot++ = *it;
}
return ot;
}
template<typename It, typename OutIt, typename Func>
OutIt printContainer(It begin, It end, OutIt ot, Func F) {
for(auto it=begin; it != end; ++it) {
if (F(*it)) {
*ot++ = *it;
}
}
return ot;
}
int main() {
vector<int> v { 1, 4, 5, 19, 2 };
printContainer(v.begin(), v.end(), ostream_iterator<int>(cout, " "), [](auto x) {
if(x % 2 == 0) {
return true;
}
return false;
});
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment