Search code examples
c++iterator

iterator as variable data type used in collection


I'd like to use an iterator as a variable type in a collection, let's assume map. I've tried this:

#include <unordered_map>
#include <iterator>

using namespace std;

void f () {
  unordered_map<int,Iterator> m;
}

and also this:

template <typename M>
 unordered_map<int,M> m;

Not sure how to approach it


Solution

  • There isn't a singular Iterator type in C++. It is a categorisation of types, based on their behaviour.

    Each container type defines a number of iterator types, e.g. std::vector<int>::iterator is different from std::vector<float>::iterator, and also different from std::list<int>::iterator, and also different from std::vector<int>::const_iterator, although in the latter case iterator is convertible to const_iterator.

    As well as containers' iterator types, there are also various iterators defined in <iterator> <ranges>, and other user defined iterators. Also each pointer type is an iterator.

    You have to choose which type you want in the map.