Open In App
Related Articles

C++ program to find the type of the given iterator

Improve Article
Save Article
Like Article

Given a program that uses an iterator, the task is to find the type of iterator used.


Input : vector.begin()
Output : Random_Access Iterator

Input : list.begin()
Output : Bidirectional Iterator

There are namely five types of iterators present in the C++ Standard Library which are mentioned below:

  1. Forward Iterator in C++
  2. Bidirectional_Iterators in C++
  3. Input iterators in C++
  4. Output iterator in C++
  5. Random_access iterators in C++


  • Iterator type can be checked by using typeid. typeid is a C++ language operator which returns type identification information at run time. It basically returns a type_info object, which is equality-comparable with other type_info objects.
  • Along with it use iterator traits. Traits class defines properties of iterators. Standard algorithms determine certain properties of the iterators passed to them and the range they represent by using the members of the corresponding iterator_traits instantiation.
  • Also a iterator category is passed which defines the iterator category the iterator belongs to.
    There are five type of tags namely: input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, random_access_iterator_tag.
  • typename is used along with it to provide a type to the iterator during instantiation.
  • Now if the iterator category of the input iterator matches the existing iterator categories the result is displayed.

// C++ program to find the type of iterator
#include <bits/stdc++.h>
using namespace std;
template <class T>
// function to return the iterator type
string get_iterator_type(T it)
    // if the iterator category of (it) matches input
    if (typeid(typename iterator_traits<T>::iterator_category)
        == typeid(input_iterator_tag))
        return "Input";
    // if the iterator category of (it) matches output
    else if (typeid(typename iterator_traits<T>::iterator_category)
             == typeid(output_iterator_tag))
        return "Output";
    // if the iterator category of (it) matches forward
    else if (typeid(typename iterator_traits<T>::iterator_category)
             == typeid(forward_iterator_tag))
        return "Forward";
    // if the iterator category of (it) matches bidirectional
    else if (typeid(typename iterator_traits<T>::iterator_category)
             == typeid(bidirectional_iterator_tag))
        return "Bidirectional";
    // if the iterator category of (it) matches random_access
    else if (typeid(typename iterator_traits<T>::iterator_category)
             == typeid(random_access_iterator_tag))
        return "Random_Access";
    // if the iterator category of (it)
    // does not match any of the above
    return "Missing";
// Driver code
int main()
    vector<int> v;
    // iterator that will be checked
    auto it = v.begin();
    cout << get_iterator_type(it) << " Iterator\n";
    return 0;


Random_Access Iterator

Time-Complexity: O(1) to find the iterator type

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 23 Dec, 2022
Like Article
Save Article
Similar Reads