C++ STL iterator_traits::random_access_iterator_tag class program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ random_access_iterator_tag class which is a class that provides a return type for iterator_category function that represents a random-access iterator

To show: How to use the C++ iterator_traits::random_access_iterator_tag class which provides a return type for iterator_category function that represents a random-access iterator in C++ programming

 

// C++ STL iterator_traits::random_access_iterator_tag class

#include <iterator>

#include <vector>

#include <iostream>

#include <list>

using namespace std;

 

int main(void)

{

// vectors and list containers

vector<int> vi;

vector<char> vc;

list<char> lc;

 

// iterator_category

typedef iterator_traits<vector<int>:: iterator>::iterator_category cati;

iterator_traits<vector<char>:: iterator>::iterator_category catc;

typedef iterator_traits<list<char>:: iterator>::iterator_category catlc;

 

// these are both random-access iterators

cout<<"The type of iterator for vector<int> is identified by the tag: "<<endl<<typeid(cati).name()<<endl;

cout<<"The type of iterator for vector<char> is identified by the tag: "<<endl<<typeid(catc).name()<<endl;

cout<<"\nOperation: typeid(cati) == typeid(catc)?"<<endl;

if (typeid(cati) == typeid(catc))

cout<<"The iterators are the same."<<endl<<endl;

else

cout<<"The iterators are not the same."<<endl<<endl;

 

// but the list iterator is bidirectinal, not random access

cout<<"The type of iterator for list<char> is identified by the tag: "<<endl<<typeid(catlc).name()<<endl;

// cout<<(typeid(vi.begin()) == typeid(vc.begin()))<<endl;

cout<<"\nOperation: typeid(vi.begin()) == typeid(vc.begin())?"<<endl;

if (typeid(vi.begin()) == typeid(vc.begin()))

cout<<"The iterators are the same."<<endl;

else

cout<<"The iterators are not the same."<<endl;

 

// a random-access iterator is a bidirectional iterator.

cout<<"\nThe address?: "<<(void*)dynamic_cast<iterator_traits<list<char>:: iterator>::iterator_category* > (&catc)<<endl;

return 0;

}

 

Output examples:

 

The type of iterator for vector<int> is identified by the tag:

struct std::random_access_iterator_tag

The type of iterator for vector<char> is identified by the tag:

struct std::random_access_iterator_tag

Operation: typeid(cati) == typeid(catc)?

The iterators are the same.

The type of iterator for list<char> is identified by the tag:

struct std::bidirectional_iterator_tag

Operation: typeid(vi.begin()) == typeid(vc.begin())?

The iterators are not the same.

The address?: 0012FEFF

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index