开发者

Vector declaration type in c++

Please can an开发者_StackOverflow社区ybody explain to me what this means?

vector<int> myvector(4,99);  


It's (most likely) an std::vector of integers, initialized to contain four integers with the value of 99.


A a(x,y); creates an object called a, calling a constructor of A with two parameters matching the types of x and y, or any convertible types.

So this:

vector<int> myvector(4,99);

Matches this constructor:

explicit vector( size_type num, const TYPE& val = TYPE() ); 
// `TYPE` is a `typedef` assigned to the parametrized type (here `int`), which means the constrcutor is actually:
explicit vector( size_type num, const int& val = int() );

Which constructs a vector with 4 elements of value 99 and calls it myvector. This constructor is called because the first parameter can be converted to a size_type, which is also a typedef, defined to an integral type (usually unsigned long).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜