Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2014 02:02
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 anonymous/11163040 to your computer and use it in GitHub Desktop.
Save anonymous/11163040 to your computer and use it in GitHub Desktop.
#include <boost/tuple/tuple.hpp>
#include <boost/iterator/iterator_facade.hpp>
struct triple_iterator: public boost::iterator_facade
<triple_iterator, boost::tuple<int, int, int> const,
boost::random_access_traversal_tag>
{
int m;
value_type value;
triple_iterator(): m(1), value{3, 4, 5} {
}
bool equal (triple_iterator const& o) const {
return m == o.m;
}
difference_type distance_to (triple_iterator const& o) const {
return o.m - m;
}
void increment() { ++m; calc(); }
void decrement() { --m; calc(); }
void advance (difference_type x) { m += x; calc(); }
reference dereference() const { return value; }
void calc() {
auto const n = m + 1;
auto const nn = n*n;
auto const mm = m*m;
auto const nm = n*m;
value = value_type { nn - mm, 2 * nm, nn + mm };
}
};
#include <algorithm>
#include <iterator>
#include <iostream>
#include <boost/tuple/tuple_io.hpp>
namespace tuples = boost::tuples;
int main() {
std::copy_n (triple_iterator(), 10, std::ostream_iterator
<triple_iterator::value_type>(std::cout, "\n"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment