tags:

views:

448

answers:

6
typedef set<int, less<int> > SetInt;

Please explain what this code does.

+22  A: 

This means that whenever you create a SetInt, you are actually creating an object of set<int, less<int> >.

For example, it makes the following two pieces of code equivalent:

SetInt somevar;

and

set<int, less<int> > somevar;
Macha
One should also note that this is an exact synonym, not a new type being introduced. It means that any specialization of templates for SetInt will also apply to set<int, less<int> >. Therefore, it's just a short hand, which is useful for readability or DRY for example.
Matthieu M.
A: 

It makes an alias to the type called SetInt, which is equivalent to set<int, less<int> >.

About your question about less, that refers to std::less, the comparer that set will use to sort your objects.

GMan
+3  A: 

You can just use SetInt after the typedef as if you are using set<int, less<int>>. Of course, typedef is scope aware.

AraK
You still need a space in '> >', though the C++0x standard may make that unnecessary in the future.
Jonathan Leffler
+4  A: 

From Wikipedia:

typedef is a keyword in the C and C++ programming languages. It is used to give a data type a new name. The intent is to make it easier for programmers to comprehend source code.

In this particular case, it makes SetInt a type name, so that you can declare a variable as:

SetInt myInts;
dcrosta
A: 

A typedef in C/C++ is used to give a certain data type another name for you to use.

In your code snippet, set<int, less<int> > is the data type you want to give another name (an alias if you wish) to and that name is SetInt

The main purpose of using a typedef is to simplify the comprehension of the code from a programmer's perspective. Instead of always having to use a complicated and long datatype (in your case I assume it is a template object), you can choose a rather simple name instead.

Partial
A: 

The code means that you give an alias or name (SetInt) to the

set<int, less<int>>

object...i.e. instead of always calling the object as

set<int, less<int>>

you can just give SetInt as the name and call the object.... just like

int i;

eg:

SetInt setinteger;
Light_handle
You still need a space in '> >', though the C++0x standard may make that unnecessary in the future.
Jonathan Leffler