简体   繁体   English

请给我解释两行

[英]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. 第一行创建 vector<double>::size_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. typedef关键字通常用于创建“新”数据类型名称,该名称通常比原始名称短,或者给定应用程序的名称更清晰。

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. typedef的高清 INES一个类型 ,这样你至少可以在这个例子中使用,而不是再旧的这个新名字。 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. 最后,此size变量的值设置为作业对象的大小,也可能是矢量。

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?" . vector<double>::size_type已经作为一个整数类型进行类型定义(读为“如果我有一个'double'元素的向量,那么它的大小将使用什么?”

Typedef'ing it further to vec_sz makes sense to shorten the type name. 将其进一步定义为vec_sz可以缩短类型名称。 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 . 类向量为size_type发布typedef Your first line redefines that to the shorter notation vec_sz . 您的第一行将其重新定义为较短的符号vec_sz vector also defines a member function size() as returning size_type . vector还将成员函数size()定义为返回size_type

Ok, inside vector<>'s declaration you'll find this: 好的,在vector <>的声明中,您将找到以下内容:

typedef unsigned int size_type; typedef unsigned int size_type; (it's actually dependant on your implementation, so it could be other than unsigned int). (它实际上取决于您的实现,因此可能不是unsigned int)。

So now you have a size_type type inside vector. 所以现在您在vector中有一个size_type类型。

"typedef vector::size_type vec_sz;" “ typedef vector :: size_type vec_sz;” would now be the same as saying: 现在将与说相同:

typedef unsigned int vec_sz; typedef unsigned int vec_sz;

Now "vector::size_type" is synonym for "unsigned int", remember that size_type is a type, not a variable. 现在,“ vector :: size_type”是“ unsigned int”的同义词,请记住,size_type是类型,而不是变量。

vec_sz size = homework.size(); vec_sz size = homework.size();

Is equal to: 等于:

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

Wich is equal to: Wich等于:

unsigned int size = homework.size(); 无符号整数大小= homework.size();

Hope it's clear :P 希望很清楚:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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