Reputation: 1535
My background is mostly in R, SAS, and VBA, and I'm trying to learn some C++. I've picked "Accelerated C++" (Koenig, Moo) as one of my first books on the subject. My theoretical background in comp. sci. is admittedly not the most robust, which perhaps explains why I'm confused by points like these.
I have a question about a piece of code similar to the following:
#include <iostream>
#include <vector>
int main() {
double input;
std::vector<double> example_vector;
while (std::cin >> input) {
example_vector.push_back(input);
}
std::vector<double>::size_type vector_size;
vector_size = example_vector.size();
return 0;
}
As I understand it, vector_size
is "large enough" to hold the size of example_vector
, no matter how large example_vector
might be. I'm not sure I understand what this means: is vector_size
(in this case) capable of representing an integer larger than, say, long long x;
, so that std::cout << vector_size;
would print a value that's different from std::cout << x;
? How/why?
Upvotes: 2
Views: 268
Reputation: 20741
In the vector class (and many others) they have a typedef:
typedef **implementation_defined** size_type;
A "size_type" may or may not use size_t. That said, size_t is always large enough to cover all your memory (32 bits or 64 bit as might be.) Note that it is likely that size_t will always be large enough to cover all your memory (at least as long as you program on your desktop computer), but that is not guaranteed.
Additionally, to know the size of a variable, and thus how much it can hold, you use sizeof.
So this would give you the actual size of the size_type type:
std::cout << sizeof(std::vector<double>::size_type) << std::endl;
which is going to be 4 or 8 depending on your processor and compiler. The maximum size is defined as 2 power that sizeof x 8 (i.e. 2 power 32 or 2 power 64.)
Upvotes: -1
Reputation: 10333
std::vector<T>::size_type
is exactly what the standard implies, the type currently used by your implementation to allow storing the size.
Historically architectures have changed, requiring code parsers, retesting and other basic wastes of time. By following the standards, your code will work on any complient platform, now and in the future.
So your code can work on 16bit archetectures and theoretically 256bit architectures at such time as they are supported.
So, while you are most likely only going to see it as size_t
if you change platforms, you don't have to worry. And more importantly, whoever is maintaining your code doesn't have to either.
Upvotes: 0
Reputation: 613003
What this question boils down to is that the standard does not mandate what actual type is returned by the vector<T>::size()
method. Different implementations may make different choices.
So, if you wish to assign the value returned by a call to size()
to a variable, what type should you use for that variable? In order to write code that is portable across different implementations, you need a way to name that type, recognising the fact that different implementations of the standard library may use different types.
The answer is that vector<T>
provides the type that you should use. It is
vector<T>::size_type
One thing that you do need to understand, and get used to, with C++, is that the standard does need to cater for significant variation between different implementations.
Upvotes: 3