[ACCEPTED]-What does typedef do in C++-typedef

Accepted answer
Score: 30

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

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

SetInt somevar;


set<int, less<int> > somevar;
Score: 6

From Wikipedia:

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

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

SetInt myInts;
Score: 3

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

Score: 0

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

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

Score: 0

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

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

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

Score: 0

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

set<int, less<int>>

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

set<int, less<int>>

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

int i;


SetInt setinteger;

More Related questions