Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Created January 25, 2015 08:54
Show Gist options
  • Save zaltoprofen/725e7cfce8c54054d5ce to your computer and use it in GitHub Desktop.
Save zaltoprofen/725e7cfce8c54054d5ce to your computer and use it in GitHub Desktop.
#include <iostream>
#include <type_traits>
#include <iterator>
template<typename InputIterator, typename Predicate>
bool all_of(InputIterator begin, InputIterator end, Predicate pred){
static_assert(std::is_integral<typename std::result_of<Predicate(typename std::iterator_traits<InputIterator>::value_type)>::type>::value, "pred should return integral");
while (begin != end){
if(!pred(*begin++)) return false;
}
return true;
}
int main(int argc, char const* argv[])
{
int dat1[] = {0,2,4,6,8};
int dat2[] = {0,2,4,5,8};
auto is_even = [](int i){return i % 2 == 0;};
std::cout << all_of(dat1, dat1 + 5, is_even) << std::endl;
std::cout << all_of(dat2, dat2 + 5, is_even) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment