Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created April 20, 2019 05:00
Show Gist options
  • Save MikuroXina/9c982569cd3c45b239d4ce3cb36e2830 to your computer and use it in GitHub Desktop.
Save MikuroXina/9c982569cd3c45b239d4ce3cb36e2830 to your computer and use it in GitHub Desktop.
The iterator has index by pair.
#ifndef INDEX_IT_LIB
#define INDEX_IT_LIB
template<class Iterator>
class index_iterator {
size_t _i = 0;
Iterator _it;
public:
index_iterator(Iterator it) : _it(it) {}
auto operator*() { return std::make_pair(_i, *_it); }
auto operator++() { ++_i; ++_it; return std::make_pair(_i, _it); }
auto operator+(size_t amount) const {
auto adv = index_iterator<Iterator>(_it + amount);
adv._i = _i + amount;
return adv;
}
bool operator!=(index_iterator<Iterator> const &r) const { return _it != r._it; }
};
#endif // INDEX_IT_LIB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment