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.
Yes, this is the required behavior.
T()for any numeric typeTyields0(of typeT, of course).This is called value initialization, which for numeric types is the same as zero initialization.