开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜