How can I know which iterator type I am using?

Reading the textbook of C++, I know that there are 5 iterator types (input, output, forward, bidirection, and random) in STL. How can I know which type I am using? For example,

void main()
{
vector<int> v;
v.push_back(1);
v.push_back(2);

vector<int>::iterator p;
p=v.begin();
while(p!=v.end()){
cout<< *p << endl;
p++;
}
}

How can I know which iterator type of p in the above program?

Thank you.
To know about the properties of an iterator (typically in generic code), use std::iterator_traits
http://en.cppreference.com/w/cpp/iterator/iterator_traits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <iterator>
#include <typeinfo>
#include <vector>
#include <list>
#include <set>
#include <experimental/filesystem>

void dump_category( std::random_access_iterator_tag )
{ std::cout << "category: random_access_iterator\n" ; }

void dump_category( std::bidirectional_iterator_tag )
{ std::cout << "category: bidirectional_iterator\n" ; }

void dump_category( std::forward_iterator_tag )
{ std::cout << "category: forward_iterator\n" ; }

void dump_category( std::input_iterator_tag )
{ std::cout << "category: input_iterator\n" ; }

void dump_category( std::output_iterator_tag )
{ std::cout << "category: output_iterator\n" ; }

template < typename ITERATOR > void iter_info()
{
    using traits = std::iterator_traits<ITERATOR> ;
    dump_category( typename traits::iterator_category{} ) ;

    std::cout << "iterates over values of type: "
              << typeid( typename traits::value_type).name()
              << "\n----------------------------\n" ;
}

template < typename ITERATOR > void iter_info(ITERATOR) { iter_info<ITERATOR>() ; }

#define ITER_INFO(TYPE) { std::cout << "iterator type: " << #TYPE << '\n' ; iter_info<TYPE>() ; }

int main()
{
    ITER_INFO( int* ) ;
    ITER_INFO( std::vector<double>::iterator ) ;
    ITER_INFO( std::list<std::string>::iterator ) ;
    ITER_INFO( std::set<char>::iterator ) ;
    ITER_INFO( std::experimental::filesystem::directory_iterator ) ;
}

iterator type: int*
category: random_access_iterator
iterates over values of type: int
----------------------------
iterator type: std::vector<double>::iterator
category: random_access_iterator
iterates over values of type: double
----------------------------
iterator type: std::list<std::string>::iterator
category: bidirectional_iterator
iterates over values of type: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
----------------------------
iterator type: std::set<char>::iterator
category: bidirectional_iterator
iterates over values of type: char
----------------------------
iterator type: std::experimental::filesystem::directory_iterator
category: input_iterator
iterates over values of type: class std::experimental::filesystem::v1::directory_entry
----------------------------

http://rextester.com/YAZEEM52676
Apologies in advance as I've not 100% understand the above solution (specially line 34 – it looks like recursion but I don't think it is, hence bit unsure there), so please forgive me if I'm repeating it in some some way …

In the following approach I've overloaded the template function for each of the iterator types and then created a second template function that calls the corresponding overload of the first template function depending on the iterator type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iterator>
#include <typeinfo>
#include <vector>
#include <list>
#include <string>

template <typename Iterator>
void iter_info(Iterator itr, std::random_access_iterator_tag)
{
        std::cout << "Random Access iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::bidirectional_iterator_tag)
{
        std::cout << "Bidirectional iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::forward_iterator_tag)
{
        std::cout << "Forward iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::input_iterator_tag)
{
        std::cout << "Input iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::output_iterator_tag)
{
        std::cout << "Output iterator \n";
}

template <typename Iterator>
void iter_info_display (Iterator itr)
{
    using category =  typename std::iterator_traits<Iterator>::iterator_category;
    return iter_info(itr, category());//category ctor call 
}

int  main()
{
    std::vector<int> v{1, 2};
    auto itr = v.begin();

    using listIter = std::list<std::string>::iterator;
    listIter list_Iter;

    iter_info_display(itr);//prints ramdom access
    iter_info_display(list_Iter);//prints bi-directional
}

Apologies in advance as I've not 100% understand the above solution (specially line 34 – it looks like recursion but I don't think it is, hence bit unsure there)

OH! I get it now!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <iterator>
#include <typeinfo>
#include <vector>
#include <list>
#include <string>

template <typename Iterator>
void iter_info(Iterator itr, std::random_access_iterator_tag)
{
        std::cout << "Random Access iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::bidirectional_iterator_tag)
{
        std::cout << "Bidirectional iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::forward_iterator_tag)
{
        std::cout << "Forward iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::input_iterator_tag)
{
        std::cout << "Input iterator \n";
}
template <typename Iterator>
void iter_info(Iterator itr, std::output_iterator_tag)
{
        std::cout << "Output iterator \n";
}

template <typename Iterator>
void iter_info (Iterator itr)
{
    using category = typename std::iterator_traits<Iterator>::iterator_category;
    return iter_info<Iterator>(itr, category());
}


int  main()
{
    std::vector<int> v{1, 2};
    auto itr = v.begin();

    using listIter = std::list<std::string>::iterator;
    listIter list_Iter;

    iter_info(itr);//prints ramdom access
    iter_info(list_Iter);//prints bi-directional
}
Topic archived. No new replies allowed.