typedef and container iterators

I got this statement from some code in a book but I was looking for some clarification:

 
 typedef list <string>::iterator IT;

I'm a bit confused about what this actually means. Does this mean IT is a string iterator? Why use typedef in this situation, why not just say:
 
list <string>::iterator it;

and now 'it' is a string iterator, just as before in the typedef example?

Thank you
you are defining the word IT as type which is of type list<string>::iterator which would be an iterator to a list of strings. NOT an iterator of a string in a list a string iterator looks like this
string::iterator

*edit

On a side note when I say you are defining it basically its like this

1
2
3
4
5
typedef list<string>::iterator IT;

IT apple; //apple is now of type - list<string>::iterator
//it is the same as this next line
list<string>::iterator apple;


You can also do it with other data types.

1
2
3
typedef int I;
typedef int a;
I am; a number;


*edit 2

The main use for typedef AFAIK is to minimize code

ex:

instead of calling something like this several times

std::pair<std::pair<int , int> , std::vector<unsigned long long> >::const_reverse_iterator

You could typdef it or just use the auto keyword.
Last edited on
also this is possible in C++11:
1
2
3
using IT = list<string>::iterator;
//Same as
typedef list<string>::iterator IT;
Last edited on
Topic archived. No new replies allowed.