explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
vector<int> vec(10);
cout << "vec.size: " << vec.size() << endl;
for (vector<int>::const_iterator iter=vec.begin(); iter != vec.end(); ++iter)
{
cout << *iter << endl;
}
Output from VS2010:
vec.size: 10
0
0
0
0
0
0
0
0
0
0
Question>: Based on the latest C++ standard, what is the default int value when we define an object of vector by using vectorObject(size_type)?
Here as you can see, VS2010 outputs 0 as the default int value. But I don't know whether or not this is required by C++ standard.
The default value of constant variables are zero. A program that demonstrates the declaration of constant variables in C using const keyword is given as follows.
According to ESR, const was added in the ANSI C Draft Proposed Standard. Eric Giguere's summary of ANSI C, dated 1987, confirms it.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ).
Yes, this is the required behavior. T()
for any numeric type T
yields 0
(of type T
, of course).
This is called value initialization, which for numeric types is the same as zero initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With