简体   繁体   中英

Please explain two lines to me

typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();

The first line creates an alias of the vector<double>::size_type type. The typedef keyword is often used to make "new" data types names that are often shorter than the original, or have a clearer name for the given application.

The second line should be pretty self-explanatory after that.

如果您了解STL容器的基础知识,它们就是老师给您测试的示例行。

typedef def ines a type so you can use this new name instead of the longer old one, at least in this example. Then a variable size is defined, and it's type is of the just defined kind. At last the value of this size variable is set the the size of the homework object, probably also a vector.

vector<double>::size_type is already typedef'd as one integral type (this reads as "If I had a vector of 'double' elements, what would you use for its size?" .

Typedef'ing it further to vec_sz makes sense to shorten the type name. Therefore,

vec_sz size;

is equivalent to:

vector<double>::size_type size;

which is equivalent to whatever integral type is used for size, for example

unsigned long size;

The class vector publishes a typedef for size_type . Your first line redefines that to the shorter notation vec_sz . vector also defines a member function size() as returning size_type .

Ok, inside vector<>'s declaration you'll find this:

typedef unsigned int size_type; (it's actually dependant on your implementation, so it could be other than unsigned int).

So now you have a size_type type inside vector.

"typedef vector::size_type vec_sz;" would now be the same as saying:

typedef unsigned int vec_sz;

Now "vector::size_type" is synonym for "unsigned int", remember that size_type is a type, not a variable.

vec_sz size = homework.size();

Is equal to:

vector::size_type size = homework.size();

Wich is equal to:

unsigned int size = homework.size();

Hope it's clear :P

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM