Hans Lustiga
Hans Lustiga

Reputation: 37

Create a vector in C++ with constant values

I want to create a vector by determining the size and the constant value which the vector has, e.g. a vector of the size 5 and only 3 as values.

vector = {3, 3, 3, 3, 3} 

I only know how to create a vector with zero as value with std::vector<int> vec(5);

Upvotes: 1

Views: 2818

Answers (1)

Urh
Urh

Reputation: 187

You can use a constructor that accepts a count and a value.

std::vector<int> v (5,3);  

Will result in having a vector of a size 5 and 3 as a values {3,3,3,3,3}.

Upvotes: 2

Related Questions