It isn’t needed in C++, and here’s why:
C# only supports dynamic polymorphism. So to create a reusable algorithm, you need an interface which all iterators will implement. That’s IEnumerator<T>, and IEnumerable<T> is a factory for returning an iterator.
C++ templates, on the other hand, support duck typing. That means you don’t need to constrain a generic type parameter by an interface in order to access members — the compiler will look up members by name for each individual instantiation of the template.
C++ containers and iterators have implicit interfaces which is equivalent to .NET IEnumerable<T>, IEnumerator<T>, ICollection<T>, IList<T>, namely:
For containers:
iteratorandconst_iteratortypedefsbegin()member function — fills the need forIEnumerable<T>::GetEnumerator()end()member function — instead ofIEnumerator<T>::MoveNext()return value
For forward iterators:
value_typetypedefoperator++— instead ofIEnumerator<T>::MoveNext()operator*andoperator->— instead ofIEnumerator<T>::Current- reference return type from
operator*— instead ofIList<T>indexer setter operator==andoperator!=— no true equivalent in .NET, but with container’send()matchesIEnumerator<T>::MoveNext()return value
For random access iterators:
operator+,operator-,operator[]— instead ofIList<T>
If you define these, then standard algorithms will work with your container and iterator. No interface is needed, no virtual functions are needed. Not using virtual functions makes C++ generic code faster than equivalent .NET code, sometimes much faster.
Note: when writing generic algorithms, it’s best to use std::begin(container) and std::end(container) instead of the container member functions. That allows your algorithm to be used with raw arrays (which don’t have member functions) in addition to the STL containers. Raw arrays and raw pointers satisfy all other requirements of containers and iterators, with this single exception.