开发者

What does typedef do in C++

typedef set<int, less<int> > SetInt;
开发者_如何转开发

Please explain what this code does.


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;


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;


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


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.


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜