#include #include #include using namespace std; template > class Cycler { public: Cycler(Container & c) : cont{c}, current{c.begin()} {} T & next() { T & val = *current; if ( ++current == cont.end() ) { current = cont.begin(); } return val; } void reset() { current = cont.begin(); } typename Container::size_type size() const { return cont.size(); } private: Container & cont; decltype(cont.begin()) current; // or Container::iterator }; // Tries both vector and other container with other type int main() { vector vals {1,4,2,67}; Cycler c1{vals}; for (int i{}; i < 10; ++i) { cout << c1.next() << endl; } list vals2 {"hi", "hello", "foo"}; Cycler> c2{vals2}; for (int i{}; i < 10; ++i) { cout << c2.next() << endl; } }