Skip to content

Instantly share code, notes, and snippets.

@qookei
Created December 7, 2019 22:51
Show Gist options
  • Save qookei/5941cd74b2ec8f931b715de91195ea4f to your computer and use it in GitHub Desktop.
Save qookei/5941cd74b2ec8f931b715de91195ea4f to your computer and use it in GitHub Desktop.
template <typename Cont>
struct enumeration {
enumeration(Cont &c)
:_c{c}, _begin{c.begin(), 0}, _end{c.end(), c.size()}
{}
private:
struct enumeration_iterator {
typename Cont::iterator it;
typename Cont::size_type n;
enumeration_iterator &operator++() {
it++;
n++;
return *this;
}
auto operator*() {
struct {
typename Cont::size_type n;
typename Cont::value_type &v;
} e{n, *it};
return e;
}
bool operator!=(enumeration_iterator &other) {
return it != other.it || n != other.n;
}
};
public:
enumeration_iterator begin() {
return _begin;
}
enumeration_iterator end() {
return _end;
}
private:
Cont &_c;
enumeration_iterator _begin, _end;
};
template <typename Cont>
enumeration<Cont> enumerate(Cont &c) {
return enumeration<Cont>{c};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment