syntax question : std::vector<int> vec (256, -1);

Hello,

I'm trying to understand this : (256, -1) , in this line :

 
  std::vector<int> vec (256, -1);



Everywhere I look on std::vector, I never see this kind of syntax.

I only understand that vec is initialized to (256, -1).
Last edited on
1
2
3
4
5
6
7
8
9
10
// An empty vector.
std::vector<int> vec1;

// A vector containing 256 elements that are
// all initialized to the default value 0.
std::vector<int> vec2(256);

// A vector containing 256 elements that are
// all initialized to the specified value -1.
std::vector<int> vec3(256, -1);
http://www.cplusplus.com/reference/vector/vector/vector/
1
2
3
4
5
6
7
8
9
10
11
vector();
explicit vector (const allocator_type& alloc);
explicit vector (size_type n, const allocator_type& alloc = allocator_type());
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
template <class InputIterator>
  vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
vector (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());

The (256, -1) has two values, both int, and int is not an allocator, nor an iterator.
That reduces the list:
1
2
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
vector (initializer_list<value_type> il);


The initializer_list appeared with brace-init syntax in C++11:
http://en.cppreference.com/w/cpp/utility/initializer_list

That leaves us with:
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
which, due to default value looks like:
vector (size_type n, const value_type& val);
(2) fill constructor
Constructs a container with n elements. Each element is a copy of val.
Thank you !
Topic archived. No new replies allowed.