stl - How to implement "dereference and post-increment" for input iterator in C++? -
requirements inputiterator include *i++
equivalent expression being
value_type x = *i; ++i; return x;
how can 1 declare such operator without implementing standard post-increment i++
returning non-void value (which inputiterators not required do)?
you may use proxy post increment:
#include <iostream> class input_iterator { private: class post_increment_proxy { public: post_increment_proxy(int value) : value(value) {} int operator * () const { return value; } private: int value; }; public: post_increment_proxy operator ++ (int) { post_increment_proxy result{value}; ++value; return result; } private: int value = 0; }; int main() { input_iterator i; std::cout << *i++ << '\n'; std::cout << *i++ << '\n'; std::cout << *i++ << '\n'; }
Comments
Post a Comment