std::vector size?
c++, c++11, initializer-list, std, vector
Solution
You're not calling the constructor that sets the vector to an initial size.
std::vector<int> v{size};
The above creates a `vector` containing a single `int` element with the value `size`. You're calling this constructor:
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() );
The braced-initializer list gets deduced as an `std::initializer_list<size_type>` and then a narrowing conversion must be performed since the `vector` itself contains `int`s.
To set the initial size of the vector use:
std::vector<int> v(size); // parentheses, not braces
Also, the `vector` constructor you've listed no longer exists, it was removed in C++11 and replaced by the following two constructors:
vector( size_type count, const T& value, const Allocator& alloc = Allocator());
explicit vector( size_type count );
cppreference.com is a much better reference as compared to cplusplus.com.
Problem
Program: ``` #include<vector> int main() { std::vector<int>::size_type size=3; std::vector<int> v{size}; } ``` when compiled with ``` g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 ``` generates error: ``` ppp.cpp: In function ‘int main()’: ppp.cpp:5:28: error: narrowing conversion of ‘size’ from ‘std::vector<int>::size_type {aka long unsigned int}’ to ‘int’ inside { } [-fpermissive] ppp.cpp:5:28: error: narrowing conversion of ‘size’ from ‘std::vector<int>::size_type {aka long unsigned int}’ to ‘int’ inside { } [-fpermissive] ``` and on http://www.cplusplus.com/reference/stl/vector/vector/ it is written ``` explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); ``` I expected that constructor to be used. Can somebody explain?