C++ program to find the type of the given iterator Last Updated : 23 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a program that uses an iterator, the task is to find the type of iterator used. Examples: 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: Forward Iterator in C++ Bidirectional_Iterators in C++ Input iterators in C++ Output iterator in C++ Random_access iterators in C++ Approach: 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. CPP // 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; } Output: Random_Access Iterator Time-Complexity: O(1) to find the iterator type Comment More infoAdvertise with us Next Article C++ program to find the type of the given iterator S Subash23 Follow Improve Article Tags : Misc C++ cpp-iterator STL cpp-operator C++-Templates +2 More Practice Tags : CPPcpp-operatorMiscSTL Similar Reads regex_iterator() function in C++ STL regex_iterator() is a function from the BiDirectionalIterator class in C++. This method returns an iterator type to iterate over different matches of a same regex pattern in a sequence. Syntax: template< class BidirectionalIterator, class CharT = typename std::iterator_traits::value_type, class T 2 min read std::istream_iterator and std::ostream_iterator in C++ STL The STL is a very powerful library in C++. It is strongly built on the principles of template programming. The STL library has three main components : Containers: These classes define the data structures which are used to contain the data. The data may be stored in linked lists, or trees or arrays. 6 min read Different ways to iterate over a set in C++ Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific order. Syntax: set<datatype> setname; Here,Datatype: Set can take any data type depending on the values, e.g. int, char, float, et 5 min read Iterators Operations in C++ Iterators are pointer like objects that are used to refer iterator the STL containers. Just like pointer arithmetic, there are some operations that are allowed on iterators in C++. They are used to provide different functionalities to make iterator more powerful and versatile. In this article, we wi 9 min read make_reverse_iterator Function in C++ The std::make_reverse_iterator() is a predefined function in C++ which is used to create the reverse_iterator for the given iterator. This function helps to iterate the data in reverse order. The std::make_reverse_iterator() function is present in the <iterator> header file. Syntaxstd::make_re 2 min read Like