Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Last active October 13, 2015 17:09
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 fabiogaluppo/d008ec37e60c172a4a58 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/d008ec37e60c172a4a58 to your computer and use it in GitHub Desktop.
forward_iterator_with_predicate and test_efficiency
#include "forward_iterator_with_predicate.hpp"
#include <vector>
#include <algorithm>
#include <iostream>
template <class Iter>
void display(Iter& iter)
{
for (auto i : iter) std::cout << i << " ";
std::cout << "\n";
}
void test_efficiency()
{
auto is_even = [](int i) {
return (i & 0x1) == 0x0;
};
std::vector<int> xs = { 1, 2, 3, 4, 5, 6, 7, 8 };
auto iwp = make_iterator_with_predicate(xs, is_even);
std::vector<int> ys;
ys.resize(4);
std::copy(iwp.begin(), iwp.end(), ys.begin());
display(ys);
std::vector<int> zs;
zs.resize(4);
std::copy_if(xs.begin(), xs.end(), zs.begin(), is_even);
display(zs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment