C34nm
C34nm

Reputation: 27

What is meaning of these C++ statements typedef vector<double>::size_type

I am beginner in C++ and have question about these statements.

vector<double> vec1;
typedef vector<double>::size_type vec_size
vec_size size = vec1.size()

From question, C++: "vector<int>::size_type variable" - what is the point of declaring in this way?, I understand that size::type holds largest possible vector size. I am confused what type is size::type - is it a function, variable, etc.?

Using typedef vec_size is equivalent to vector<double>::size_type. So the third line becomes

vector<double>::size_type size = vec1.size()

This is very confusing to me. What is type of variable size?

It would be really helpful if someone comments in simplified language.

Upvotes: 0

Views: 873

Answers (3)

Sam Varshavchik
Sam Varshavchik

Reputation: 118350

The are really two questions here:

what is the point of declaring in this way

It's to keep your fingers from falling off your hand. If you need to reference this type over and over again, it's easier to just type vec_size each time, instead of spelling out vector<double>::size_type. Each. and. every. time.

Use the typedef. Your fingers will thank you for it. Especially the little pinky finger, who doesn't have to hit the SHIFT key so often, any more.

Not to mention that it makes the resulting code easier on the eyes, too.

Now, as far as the second question:

What is type of variable size?

The answer is: you don't know, and you shouldn't care. The only thing you should care about is that this type is sufficient to represent the number of elements in the vector. That's the definition of what this type is.

It might be an unsigned int, or an unsigned long, or even an unsigned long long. But don't worry about it. Whatever it is, it will be big enough to express the size of the largest possible vector you have even the slightest hope of declaring and using. This also means that this type would also be the right type for representing an index of any element in the vector. So, if you need to save the index of some particular element in the vector, or iterate over the values in the vector by their index, this is the right type for it. Since it's big enough to represent the size of the vector, it's big enough to represent the index of any particular value in the vector.

Of course, for vectors whose size is expected to be quite modest, by today's standards, using a plain int will also work. But by using this type, you'll be bragging to all your friends: see, I know a lot about C++. I know how to use this type properly.

Upvotes: 3

Peter
Peter

Reputation: 36607

The type of vec_size is std::vector<double>::size_type. This will often be a typedef for some unsigned integral type, but the actual type is implementation defined.

In other words, it varies between compilers and their shipped standard library (and header files).

It will often be a std::size_t (or something equivalent to it) but is allowed to be something different. The actual type of std::size_t is also implementation-defined - it might be unsigned, unsigned long, unsigned long long, uint32_t, uint_64_t. It might be something else (e.g. some class type that provides required operations).

If you want to work with sizes using some "simpler" type (say, unsigned long) then you can. But then you accept the risk that your chosen type does not support all possible sizes of the vector - which can mean bugs in your program that are hard to track down. Using std::vector<double>::size_type is guaranteed to work with all compilers and standard libraries that correctly conform with the standard.

Upvotes: 0

Nicol Bolas
Nicol Bolas

Reputation: 473537

This is very confusing to me. What is type of variable size?

Your question answers itself: it is a variable of type vector<double>::size_type. Consider the following:

struct type_name
{
  typedef int inner;
};

What is type_name::inner? It's a typedef stored within the type type_name. The :: there is the C++ scope syntax; it's used to refer to names which are declared within other names. Here, we declare inner within the type type_name. So its full name is type_name::inner.

In all other respects, it is no different from any other typedef.

std::vector is defined to have a type alias declared within it called size_type. Exactly what type is it an alias of? That's not stated by the standard.

What is stated is the meaning of this type. It is an unsigned integer type which is big enough to be able to index any element in the largest possible vector. at and operator[] take values of size_type; size and capacity take values of size_type. Indeed, any member of vector that relates in any way to its size or indexes elements takes a value of size_type type.

Though odds are good it's going to be a typedef of size_t.

Upvotes: 1

Related Questions