This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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