Thaier Alkhateeb
Thaier Alkhateeb

Reputation: 585

What does typedef do in C++

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

Please explain what this code does.

Upvotes: 8

Views: 14938

Answers (6)

Khaled Alshaya
Khaled Alshaya

Reputation: 96849

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

Upvotes: 3

Light_handle
Light_handle

Reputation: 4017

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;

Upvotes: 0

GManNickG
GManNickG

Reputation: 503805

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.

Upvotes: 0

Partial
Partial

Reputation: 9989

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.

Upvotes: 0

dcrosta
dcrosta

Reputation: 26258

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;

Upvotes: 6

Macha
Macha

Reputation: 14654

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;

Upvotes: 30

Related Questions