C++ standard: default "const T& value" in vector constructor for type 'int'
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 type T
yields 0
(of type T
, of course).
This is called value initialization, which for numeric types is the same as zero initialization.
精彩评论