typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
views:
274answers:
5They are example lines that your teacher gave to you test if you understood the basics of STL containers.
typedef defines 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.
Could you add a homework tag?
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 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.
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